栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

基于Spring Security令牌的身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于Spring Security令牌的身份验证

这是我能够实现基于令牌的身份验证和基本身份验证的方式

SpringSecurityConfig.java

@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter{    @Override    public void configure(final AuthenticationManagerBuilder auth) throws Exception    {        auth.userDetailsService(this.participantService).passwordEnprer(this.passwordEnprer());    }    @Override    protected void configure(final HttpSecurity http) throws Exception    {        //Implementing Token based authentication in this filter        final TokenAuthenticationFilter tokenFilter = new TokenAuthenticationFilter();        http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);        //Creating token when basic authentication is successful and the same token can be used to authenticate for further requests        final CustomBasicAuthenticationFilter customBasicAuthFilter = new CustomBasicAuthenticationFilter(this.authenticationManager() );        http.addFilter(customBasicAuthFilter);    }}

TokenAuthenticationFilter.java

    public class TokenAuthenticationFilter extends GenericFilterBean    {        @Override        public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)     throws IOException, ServletException        { final HttpServletRequest httpRequest = (HttpServletRequest)request;  //extract token from header final String accessToken = httpRequest.getHeader("header-name"); if (null != accessToken) {//get and check whether token is valid ( from DB or file wherever you are storing the token)          //Populate SecurityContextHolder by fetching relevant information using token    final User user = new User(      "username",      "password",      true,      true,      true,      true,      authorities);         final UsernamePasswordAuthenticationToken authentication =      new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());         SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response);        }      }

CustomBasicAuthenticationFilter.java

@Componentpublic class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter {    @Autowired    public CustomBasicAuthenticationFilter(final AuthenticationManager authenticationManager) {        super(authenticationManager);    }    @Override    protected void onSuccessfulAuthentication(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response, final Authentication authResult) {        //Generate Token        //Save the token for the logged in user        //send token in the response        response.setHeader("header-name" , "token");    }}

由于我们的CustomBasicAuthenticationFilter已配置并添加为Spring安全性的过滤器,

只要基本身份验证成功,请求就会重定向到onSuccessfulAuthentication,在此我们设置令牌,并在响应中使用一些标头“ header-
name”发送它。

如果发送“ header-name”以请求进一步的请求,则该请求将在尝试尝试基本身份验证之前先通过TokenAuthenticationFilter。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398272.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号