🐛 Fixing a bug. close #I4PO8H ,锁定用户异常中文处理

This commit is contained in:
lbw 2022-01-06 18:51:16 +08:00
parent 2b984b88a6
commit f723ef6f7a
8 changed files with 66 additions and 14 deletions

View File

@ -55,7 +55,7 @@ import java.util.Map;
@Configuration
@RequiredArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private final DataSource dataSource;

View File

@ -40,25 +40,19 @@ import org.springframework.security.web.authentication.logout.LogoutSuccessHandl
@Primary
@Order(90)
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
@SneakyThrows
protected void configure(HttpSecurity http) {
http.authenticationProvider(phoneAuthenticationProvider()).formLogin().loginPage("/token/login")
.loginProcessingUrl("/token/form").failureHandler(authenticationFailureHandler()).and().logout()
http.authenticationProvider(new CustomAppAuthenticationProvider())//
.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form")
.failureHandler(authenticationFailureHandler()).and().logout()
.logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true)
.and().authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll()
.anyRequest().authenticated().and().csrf().disable();
}
/**
* 不要直接使用@Bean注入 会导致默认的提供者无法注入DaoAuthenticationProvider
*/
private CustomAppAuthenticationProvider phoneAuthenticationProvider() {
return new CustomAppAuthenticationProvider();
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/css/**");

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pig4cloud.pig.common.security.component;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;
/**
* @author lengleng
* @date 2019-06-24
* <p>
* 注入自定义错误处理
*/
@ConditionalOnWebApplication(type = SERVLET)
public class PigSecurityMessageSourceConfiguration implements WebMvcConfigurer {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.addBasenames("classpath:org/springframework/security/messages");
return messageSource;
}
}

View File

@ -24,6 +24,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.oauth2.common.DefaultThrowableAnalyzer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.*;

View File

@ -2,8 +2,10 @@ package com.pig4cloud.pig.common.security.grant;
import cn.hutool.extra.spring.SpringUtil;
import com.pig4cloud.pig.common.security.service.PigUserDetailsService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AccountStatusUserDetailsChecker;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -12,6 +14,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsChecker;
import java.util.Comparator;
import java.util.Map;
@ -24,6 +27,12 @@ import java.util.Optional;
@Slf4j
public class CustomAppAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
/**
* user 属性校验
*/
@Setter
private UserDetailsChecker preAuthenticationChecks = new AccountStatusUserDetailsChecker();
/**
* 校验 请求信息userDetails
* @param userDetails 用户信息
@ -65,6 +74,10 @@ public class CustomAppAuthenticationProvider extends AbstractUserDetailsAuthenti
// 手机号
String phone = authentication.getName();
UserDetails userDetails = optional.get().loadUserByUsername(phone);
// userDeails 校验
preAuthenticationChecks.check(userDetails);
CustomAppAuthenticationToken token = new CustomAppAuthenticationToken(userDetails);
token.setDetails(authentication.getDetails());
return token;

View File

@ -69,8 +69,8 @@ public interface PigUserDetailsService extends UserDetailsService, Ordered {
// 构造security用户
return new PigUser(user.getUserId(), user.getDeptId(), user.getUsername(),
SecurityConstants.BCRYPT + user.getPassword(), user.getPhone(),
StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL), true, true, true, authorities);
SecurityConstants.BCRYPT + user.getPassword(), user.getPhone(), true, true, true,
StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL), authorities);
}
/**

View File

@ -3,5 +3,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pig.common.security.service.PigAppUserDetailsServiceImpl,\
com.pig4cloud.pig.common.security.component.PigSecurityInnerAspect,\
com.pig4cloud.pig.common.security.component.PigTokenStoreAutoConfiguration,\
com.pig4cloud.pig.common.security.component.PigTokenStoreAutoCleanSchedule
com.pig4cloud.pig.common.security.component.PigTokenStoreAutoCleanSchedule,\
com.pig4cloud.pig.common.security.component.PigSecurityMessageSourceConfiguration