Jac*_*cob 1 spring spring-security spring-boot
目前,我正在尝试使用带有弹簧安全性的spring MVC和spring boot来实现身份验证示例。在我的示例应用程序中,我想做的是-我在一个URL的标题中发送一个身份验证令牌。我需要从URL中获取此身份验证令牌并进行解码。如果用户名和密码匹配,则只需将控件转移到端点“ api / getStudent / v1”或类似的地方。否则从那里只需要给出否认的响应即可。
为此,我目前尝试使用Spring Security的身份验证提供程序。但是它不适合从请求的标头中获取令牌。在这里,我的困惑是,从spring security出发,我必须在这里实现哪种方法?谁能建议一种标准的实施方式?或任何有关此类实现的文档?
小智 5
您需要做的就是创建一个自定义安全过滤器,并在spring security BasicAuthenticationFilter之前插入此过滤器。示例代码-
public class CustomAuthenticationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
String authHeader = request.getHeaders("Authorization");
//Decode the authHeader
//Validate the authHeader with your username & password
if(invalid) {
//throw exception and abort processing
}
filterChain.doFilter(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以创建bean,也可以这样创建,@component以便spring可以为您创建bean。
在您的安全配置中,添加以下内容-
@Configuration
public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterAfter(new CustomAuthenticationFilter(), BasicAuthenticationFilter.class);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
186 次 |
| 最近记录: |