当前位置:   article > 正文

SpringBoot 2.1.1 + SpringSecurity+jwt 去掉Token验证

SpringBoot 2.1.1 + SpringSecurity+jwt 去掉Token验证

SpringBoot 2.1.1 + SpringSecurity+jwt 实现无Token验证

记录一下使用springSecurity实现jwt的授权,并对去掉鉴权认证

  • 工程创建流程
    • SecurityConfig
    • AuthenticationEntryPointImpl 和 JwtAuthenticationEntryPoint
    • JwtAuthenticationTokenFilter
    • 配置 Security信息
    • 启动类的信息

环境

  • springBoot 2.1.1
  • springSecurity 5.1.2
  • jjwt 0.9.0

pox.xml 文件主要信息

<!-- spring security 安全认证 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>
      <!--Token生成与解析-->
<dependency>
   <groupId>io.jsonwebtoken</groupId>
   <artifactId>jjwt</artifactId>
   <version>${jwt.version}</version>
</dependency>

项目结构:

修改

 spring security配置
SecurityConfig 类设置

这一个类主要设置那一些目录需要进行权限验证,其中包括登录页面、静态文件等信息不需要验证。具体过滤代码如下:

  1. protected void configure(HttpSecurity httpSecurity) throws Exception
  2. {
  3. httpSecurity
  4. // CRSF禁用,因为不使用session
  5. .csrf().disable()
  6. // 认证失败处理类
  7. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  8. // 基于token,所以不需要session
  9. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  10. // 过滤请求
  11. .authorizeRequests()
  12. // 对于登录login 验证码captchaImage 允许匿名访问
  13. .antMatchers("/login").anonymous()
  14. .antMatchers(
  15. HttpMethod.GET,
  16. "/*.html",
  17. "/**/*.html",
  18. "/**/*.css",
  19. "/**/*.js"
  20. ).permitAll()
  21. .antMatchers("/**").anonymous() //关键新增这一行即可
  22. // 除上面外的所有请求全部需要鉴权认证
  23. .anyRequest().authenticated()
  24. .and()
  25. .headers().frameOptions().disable();
  26. httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
  27. // 添加JWT filter
  28. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
  29. }

以上方法是在java中去掉所有接口带Token认证,在实际生产环境中我们肯定是不会设置的。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/495692
推荐阅读
相关标签
  

闽ICP备14008679号