java – 使用OAuth2自定义Spring Security的auth错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用OAuth2自定义Spring Security的auth错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1741字,纯文字阅读大概需要3分钟。
内容图文
![java – 使用OAuth2自定义Spring Security的auth错误](/upload/InfoBanner/zyjiaocheng/705/4259fc3b9a3f4dc59a7172795ec1b6bc.jpg)
我想知道我是否可以自定义以下授权错误:
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
我在用户请求没有权限时得到它.我想将它定制为与Spring Boot错误非常相似:
{
"timestamp":1445441285803,
"status":401,
"error":"Unauthorized",
"message":"Bad credentials",
"path":"/oauth/token"
}
有可能吗?
非常感谢.
解决方法:
我知道了 :)
https://stackoverflow.com/a/37132751/2520689
我需要创建一个实现“AuthenticationEntryPoint”的新类,如下所示:
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint
{
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException arg2) throws IOException, ServletException
{
final Map<String, Object> mapBodyException = new HashMap<>() ;
mapBodyException.put("error" , "Error from AuthenticationEntryPoint") ;
mapBodyException.put("message" , "Message from AuthenticationEntryPoint") ;
mapBodyException.put("exception", "My stack trace exception") ;
mapBodyException.put("path" , request.getServletPath()) ;
mapBodyException.put("timestamp", (new Date()).getTime()) ;
response.setContentType("application/json") ;
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED) ;
final ObjectMapper mapper = new ObjectMapper() ;
mapper.writeValue(response.getOutputStream(), mapBodyException) ;
}
}
并将其添加到我的ResourceServerConfigurerAdapter实现:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
{
@Override
public void configure(HttpSecurity http) throws Exception
{
http.exceptionHandling().authenticationEntryPoint(new AuthExceptionEntryPoint()) ;
}
}
你可以找到我的GitHub项目,它实现了你需要的一切:
https://github.com/pakkk/custom-spring-security
内容总结
以上是互联网集市为您收集整理的java – 使用OAuth2自定义Spring Security的auth错误全部内容,希望文章能够帮你解决java – 使用OAuth2自定义Spring Security的auth错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。