更改错误凭据错误响应spring security oauth2
我有一个AuthorizationServer,它使用spring security的密码授权类型。我将其用于移动应用程序,当用户输入用户名密码登录时,应用程序调用令牌端点并生成令牌(如果他/她是经过身份验证的用户)。这一切都由密码授予类型本身处理。对于不成功的登录,它将返回下面的常规错误和400 HTTP状态代码更改错误凭据错误响应spring security oauth2,spring,spring-boot,spring-security,spring-security-oauth2,Spring,Spring Boot,Spring Security,Spring Security Oauth2,我有一个AuthorizationServer,它使用spring security的密码授权类型。我将其用于移动应用程序,当用户输入用户名密码登录时,应用程序调用令牌端点并生成令牌(如果他/她是经过身份验证的用户)。这一切都由密码授予类型本身处理。对于不成功的登录,它将返回下面的常规错误和400 HTTP状态代码 { "error": "invalid_grant", "error_description": "Bad credentials" } 但对于我的场景,我需要自定义此错误
{
"error": "invalid_grant",
"error_description": "Bad credentials"
}
但对于我的场景,我需要自定义此错误消息。他们是否有办法更改此错误消息
请注意,我尝试了建议的重复问题-
但是它使用formLogin,并且它与我的实现不兼容
谢谢,
拉吉思我好几天都找不到这个问题的答案。最后,我得到了一位同事的帮助。他让我遵循这一点,这对我起了作用。现在,我可以将默认的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);
}
}
@组件
公共类CustomAuthenticationProvider实现AuthenticationProvider{
私有最终静态记录器Logger=LogManager.getLogger(CustomAuthenticationProvider.class);
@自动连线
私人用户服务;
@凌驾
公共身份验证(最终身份验证)引发AuthenticationException{
最终字符串username=authentication.getName();
最终字符串密码=authentication.getCredentials().toString();
试一试{
/*检查用户凭据*/
/*支票帐户*/
User userDetail=userService.findByUsername(用户名);
if(userDetail==null){
抛出新异常(“未找到用户!”);
}
/*检查密码*/
String origPass=Utilities.getEncrypted(新字符串(Base64.decodeBase64(密码)),用户名);
如果(!userDetail.getPassword().equals(origPass)){
抛出新异常(“错误的用户名或密码!”);
}
/*检查处于活动状态*/
如果(!userDetail.getIsActive()){
抛出新异常(“用户未激活!”);
}
/*检查腹板类型中的余量*/
if(Access.isWeb()){
如果(!userDetail.getIsWeb())
抛出新异常(“禁止Web访问!”);
}
/*检查移动类型的容差*/
if(Access.isMobile()){
如果(!userDetail.getIsMobile())
抛出新异常(“禁止移动访问!”);
}
/*做一些日志*/
userService.login(userDetail);
返回新的UsernamePasswordAuthenticationToken(userDetail,{noop}.concat(origPass),userDetail.getAuthories());
}捕获(例外e){
LOGGER.error(“[OAUTH]错误:”+e.getLocalizedMessage());
抛出新的BadCredentialsException(e.getLocalizedMessage(),e);
}
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
如果只想更改响应中的消息文本,则只需将messages.properties文件添加到应用程序的类路径中,并包含以下内容即可:
AbstractUserDetailsAuthenticationProvider.badCredentials=无效的用户名或密码
这将导致以下响应:
{
"error": "invalid_grant",
"error_description": "Invalid username or password"
}
这回答了你的问题吗@Adi否,它使用SSO和http.formLogin()。就我的情况而言,它不起作用该问题的答案也适用于SSO。抱歉@Adi我的意思是我没有使用SSO和http。formLogin()我的情况是,当用户名和密码通过移动应用程序提供时,我使用grant_类型密码登录用户。检查这是否适用于您谢谢@sebin共享此信息,似乎我也关注了上面的博文,并且成功了。