更改 Bad credentials 错误响应 spring security oauth2
Change the Bad credentials error response spring security oauth2
我有一个 AuthorizationServer,它使用密码 grant_type 使用 spring 安全性。我将其用于移动应用程序,当用户输入用户名密码登录时,如果 he/she 是经过身份验证的用户,应用程序将调用令牌端点并生成令牌。这全部由 password grant_type 本身处理。对于不成功的登录,returns 低于带有 400 HTTP 状态代码的一般错误。
{
"error": "invalid_grant",
"error_description": "Bad credentials"
}
但对于我的场景,我需要自定义此错误消息。他们有办法更改此错误消息吗?
请注意,我尝试了建议的重复问题 -
Customize authentication failure response in Spring Security using AuthenticationFailureHandler
但它使用 formLogin 并且它不适用于我的实现。
谢谢,
拉吉斯
这个问题我找了很多天都找不到答案。最后,我得到了一位同事的帮助。他让我遵循这个 tutorial 并且它对我有用。现在我可以将默认的 spring 框架响应转换为我的响应模板,如下所示。
{
"status": 400,
"message": "Invalid username or password",
"timestamp": "2020-06-19T10:58:29.973+00:00",
"payload": null
}
但是,我们仍然不知道为什么 authenticationFailure 处理程序不起作用。希望这有帮助。
Sabin 的答案有效,但我需要使用 BadCredentialsException 抛出异常,
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final static Logger LOGGER = LogManager.getLogger(CustomAuthenticationProvider.class);
@Autowired
private UserService userService;
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException{
final String username = authentication.getName();
final String password = authentication.getCredentials().toString();
try {
/* CHECKING USER CREDENTIAL */
/* check account */
User userDetail = userService.findByUsername(username);
if (userDetail == null){
throw new Exception("User not found!");
}
/* check password */
String origPass = Utilities.getEncrypted(new String(Base64.decodeBase64(password)), username);
if(!userDetail.getPassword().equals(origPass)){
throw new Exception("Wrong username or password!");
}
/* check is active */
if(!userDetail.getIsActive()){
throw new Exception("User is not active!");
}
/* check allowance in web type */
if(Access.isWeb()){
if(!userDetail.getIsWeb())
throw new Exception("Web access prohibited!");
}
/* check allowance in mobile type */
if(Access.isMobile()){
if(!userDetail.getIsMobile())
throw new Exception("Mobile access prohibited!");
}
/* do some logs */
userService.login(userDetail);
return new UsernamePasswordAuthenticationToken(userDetail, "{noop}".concat(origPass), userDetail.getAuthorities());
} catch (Exception e) {
LOGGER.error("[OAUTH] Error : " + e.getLocalizedMessage());
throw new BadCredentialsException(e.getLocalizedMessage(), e);
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
如果您只想更改响应中的消息文本,那么将 messages.properties 文件添加到您的应用程序的类路径中就足够了以下内容:
AbstractUserDetailsAuthenticationProvider.badCredentials=Invalid username or password
这将导致以下响应:
{
"error": "invalid_grant",
"error_description": "Invalid username or password"
}
我有一个 AuthorizationServer,它使用密码 grant_type 使用 spring 安全性。我将其用于移动应用程序,当用户输入用户名密码登录时,如果 he/she 是经过身份验证的用户,应用程序将调用令牌端点并生成令牌。这全部由 password grant_type 本身处理。对于不成功的登录,returns 低于带有 400 HTTP 状态代码的一般错误。
{
"error": "invalid_grant",
"error_description": "Bad credentials"
}
但对于我的场景,我需要自定义此错误消息。他们有办法更改此错误消息吗?
请注意,我尝试了建议的重复问题 - Customize authentication failure response in Spring Security using AuthenticationFailureHandler 但它使用 formLogin 并且它不适用于我的实现。
谢谢,
拉吉斯
这个问题我找了很多天都找不到答案。最后,我得到了一位同事的帮助。他让我遵循这个 tutorial 并且它对我有用。现在我可以将默认的 spring 框架响应转换为我的响应模板,如下所示。
{
"status": 400,
"message": "Invalid username or password",
"timestamp": "2020-06-19T10:58:29.973+00:00",
"payload": null
}
但是,我们仍然不知道为什么 authenticationFailure 处理程序不起作用。希望这有帮助。
Sabin 的答案有效,但我需要使用 BadCredentialsException 抛出异常,
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final static Logger LOGGER = LogManager.getLogger(CustomAuthenticationProvider.class);
@Autowired
private UserService userService;
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException{
final String username = authentication.getName();
final String password = authentication.getCredentials().toString();
try {
/* CHECKING USER CREDENTIAL */
/* check account */
User userDetail = userService.findByUsername(username);
if (userDetail == null){
throw new Exception("User not found!");
}
/* check password */
String origPass = Utilities.getEncrypted(new String(Base64.decodeBase64(password)), username);
if(!userDetail.getPassword().equals(origPass)){
throw new Exception("Wrong username or password!");
}
/* check is active */
if(!userDetail.getIsActive()){
throw new Exception("User is not active!");
}
/* check allowance in web type */
if(Access.isWeb()){
if(!userDetail.getIsWeb())
throw new Exception("Web access prohibited!");
}
/* check allowance in mobile type */
if(Access.isMobile()){
if(!userDetail.getIsMobile())
throw new Exception("Mobile access prohibited!");
}
/* do some logs */
userService.login(userDetail);
return new UsernamePasswordAuthenticationToken(userDetail, "{noop}".concat(origPass), userDetail.getAuthorities());
} catch (Exception e) {
LOGGER.error("[OAUTH] Error : " + e.getLocalizedMessage());
throw new BadCredentialsException(e.getLocalizedMessage(), e);
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
如果您只想更改响应中的消息文本,那么将 messages.properties 文件添加到您的应用程序的类路径中就足够了以下内容:
AbstractUserDetailsAuthenticationProvider.badCredentials=Invalid username or password
这将导致以下响应:
{
"error": "invalid_grant",
"error_description": "Invalid username or password"
}