Java Spring安全性-使用授权标头进行身份验证
我正在尝试使用Sprint Security framework在Spring Boot中为HTTP请求设置授权。我是Spring Security的新手,我找不到任何关于我的案例的文档 我知道我们必须重写WebSecurity配置适配器方法-configure(AuthenticationManagerBuilder)和configure(HttpSecurity)。它在一些基本情况下运行良好。然而,对于我正在进行的项目,我正在寻找一个特定的用例 这就是我试图设置的流程。我的前端和后端托管在不同的域中,因此我也在寻找跨源授权。 通过发布到RESTAPI登录,[仅此API不应有授权]Java Spring安全性-使用授权标头进行身份验证,java,spring-boot,rest,spring-security,Java,Spring Boot,Rest,Spring Security,我正在尝试使用Sprint Security framework在Spring Boot中为HTTP请求设置授权。我是Spring Security的新手,我找不到任何关于我的案例的文档 我知道我们必须重写WebSecurity配置适配器方法-configure(AuthenticationManagerBuilder)和configure(HttpSecurity)。它在一些基本情况下运行良好。然而,对于我正在进行的项目,我正在寻找一个特定的用例 这就是我试图设置的流程。我的前端和后端托管在不
https://atlan.co/login - POST
{
'user_name': 'mani',
'password': 'mani'
}
/登录REST控制器接收用户名和密码,LDAP身份验证是否在身份验证成功后创建令牌并将其存储在内存数据库[Redis]中,其中用户名、用户\电子邮件映射到令牌。并向浏览器发送登录成功消息,其中包含设置的授权Cookie-Token
之后,来自浏览器的每个请求都将伴随着授权头,以及令牌值
上面的部分,我能算出来。当请求传入时,我想设置Spring Security,这样它将读取授权头并从Redis获取用户名、useremail(如果令牌存在),将用户名、useremail传递给控制器以及控制器的常规流。在这种情况下,如果令牌不存在,则应以JSON格式抛出401个未经授权的HTTP错误以及自定义消息
我试着阅读Stackoverflow、Spring文档中的问题,但我无法解决。如果有人能够解释这一点,这将非常有帮助。您需要添加一个定制的spring筛选器来处理授权标头
public class YourAuthenticationFilter extends OncePerRequestFilter
{
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException
{
String xAuth = request.getHeader("Authorization");//here is your token value
//Place here your redis checks, get Authentication and so on
SecurityContextHolder.getContext().setAuthentication(auth);
filterChain.doFilter(request, response);
}
}
在您的安全配置中:
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
//...
.addFilterBefore(new YourAuthenticationFilter(), BasicAuthenticationFilter.class)
//...
}
我已经在我的Spring Boot应用程序中添加了上面的配置,但是我得到了403,禁止,并且流没有通过过滤器。