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

View File

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