fix(AuthExceptionHandler.java): 用户名密码自定义异常抛出

This commit is contained in:
有来技术 2021-10-27 22:43:45 +08:00
parent 877c4f0035
commit 3e6819cdf8
3 changed files with 7 additions and 7 deletions

View File

@ -37,7 +37,7 @@ public class AuthExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidGrantException.class) @ExceptionHandler(InvalidGrantException.class)
public Result handleInvalidGrantException(InvalidGrantException e) { public Result handleInvalidGrantException(InvalidGrantException e) {
return Result.failed(e.getMessage()); return Result.failed(ResultCode.USERNAME_OR_PASSWORD_ERROR);
} }

View File

@ -35,7 +35,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests().antMatchers("/oauth/**","/sms-code").permitAll() .authorizeRequests().antMatchers("/oauth/**", "/sms-code").permitAll()
// @link https://gitee.com/xiaoym/knife4j/issues/I1Q5X6 (接口文档knife4j需要放行的规则) // @link https://gitee.com/xiaoym/knife4j/issues/I1Q5X6 (接口文档knife4j需要放行的规则)
.antMatchers("/webjars/**", "/doc.html", "/swagger-resources/**", "/v2/api-docs").permitAll() .antMatchers("/webjars/**", "/doc.html", "/swagger-resources/**", "/v2/api-docs").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
@ -56,9 +56,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
public void configure(AuthenticationManagerBuilder auth) { public void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(wechatAuthenticationProvider()); auth.authenticationProvider(wechatAuthenticationProvider()).
auth.authenticationProvider(daoAuthenticationProvider()); authenticationProvider(daoAuthenticationProvider()).
auth.authenticationProvider(smsCodeAuthenticationProvider()); authenticationProvider(smsCodeAuthenticationProvider());
} }
/** /**
@ -97,14 +97,13 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Bean
public DaoAuthenticationProvider daoAuthenticationProvider() { public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false); // 是否隐藏用户不存在异常默认:true-隐藏false-抛出异常
provider.setUserDetailsService(sysUserDetailsService); provider.setUserDetailsService(sysUserDetailsService);
provider.setPasswordEncoder(passwordEncoder()); provider.setPasswordEncoder(passwordEncoder());
provider.setHideUserNotFoundExceptions(false); // 是否隐藏用户不存在异常默认:true-隐藏false-抛出异常
return provider; return provider;
} }
/** /**
* 密码编码器 * 密码编码器
* <p> * <p>

View File

@ -84,6 +84,7 @@ public class CaptchaTokenGranter extends AbstractTokenGranter {
throw new InvalidGrantException(var9.getMessage()); throw new InvalidGrantException(var9.getMessage());
} }
if (userAuth != null && userAuth.isAuthenticated()) { if (userAuth != null && userAuth.isAuthenticated()) {
OAuth2Request storedOAuth2Request = this.getRequestFactory().createOAuth2Request(client, tokenRequest); OAuth2Request storedOAuth2Request = this.getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth); return new OAuth2Authentication(storedOAuth2Request, userAuth);