
SpringBoot 2.1.1 + SpringSecurity+jwt 去掉Token验证
通过 使用 启用非会话模式,数组的用户不会创建 session。 使用 使用 启用 JWT 过滤器,确保每个请求都包含有效 Token。
发布日期:2021-05-13 19:18:39
浏览次数:16
分类:精选文章
本文共 3030 字,大约阅读时间需要 10 分钟。
Spring Boot 2.1.1 + Spring Security + JWT 实现无 Token 验证配置
项目环境
本项目基于以下环境配置:
- Spring Boot: Version 2.1.1
- Spring Security: Version 5.1.2
- JWT: Version 0.9.0
项目结构
com.example├── main│ ├── java│ │ └── com│ │ └── example│ │ ├── controller│ │ └── security│ │ ├── config│ │ ├── filter│ │ └── handler│ └── resources│ ├── static│ └── templates
安全配置
1. SecurityConfig 类设置
在 SecurityConfig
类中进行以下配置:
import org.springframework.security.config.annotation.web.HttpSecurityConfigurer;import org.springframework.security.web.authentication.authenticationEntryPoint;import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;@Configurationpublic class SecurityConfig extends HttpSecurityConfigurer { private LogoutSuccessHandler logoutSuccessHandler; @Override protected void configure(HttpSecurity httpSecurity) throws Exception { // 关键点 1:禁用 CSRF 保护 httpSecurity .csrf().disable() .exceptionHandling() .authenticationEntryPoint(unauthorizedHandler) .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .authorizeRequests() .antMatchers("/login").anonymous() .antMatchers("GET", "/**/*.html", "/**/*.css", "/**/*.js").permitAll() .antMatchers("/**").anonymous() .anyRequest().authenticated() .and() .headers().frameOptions().disable(); // 关键点 2:启用 JWT 过滤器,确保 Token 解析 httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); // 登录处理 httpSecurity.logout() .logoutUrl("/logout") .logoutSuccessHandler(logoutSuccessHandler); } // ...}
2. JWT 过滤器配置
在 JwtAuthenticationTokenFilter
类中设置 JWT 解析规则:
import io.jsonwebtoken.Jwt;import org.springframework.security.authentication.AuthenticationFilter;import org.springframework.security.authentication.AuthenticationToken;import org.springframework.security.authentication {};/** * 简单的 JWT 验证过滤器 * * @author 小天使 */public class JwtAuthenticationTokenFilter extends AnonymousAuthenticationFilter { // ...}
依赖管理
在 pom.xml
中添加以下依赖:
org.springframework.boot spring-boot-starter-security io.jsonwebtoken jjwt ${jwt.version}
关键配置点
在 SecurityConfig
中关注以下关键设置:
csrf().disable()
禁用 CSRF 保护。authenticationEntryPoint(unauthorizedHandler)
设置未授权处理。permitAll()
允许匿名访问的路径。frameOptions().disable()
禁用同源策略,允许跨域请求。备注
本配置类主要用于控制访问权限。常用的路径包括登录页面和静态资源,这些路径一般使用 permitAll()
设置为匿名访问,以提升用户体验。其他所有请求默认需要授权。
记住,以上配置仅供测试参考,在实际应用中建议添加 Token 验证中间件(如 JWT наруш者)和权限控制模块,确保系统安全性。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月23日 09时42分12秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【视频教程】EasyNVR如何将老版本的EasyNVR的数据迁移到4.0.0以上版本
2019-03-10
qt中转到槽后如何取消信号与槽关联
2019-03-10
qt问题记录-spin box与double spin box
2019-03-10
移动端事件
2019-03-10
css 图片按比例缩放
2019-03-10
小程序form表单里面buton点击事件失效
2019-03-10
微信小程序placeholder设置自定义样式
2019-03-10
安装npm install --save vue-scroller失败
2019-03-10
spring-day01
2019-03-10
spring的值注入与组件扫描
2019-03-10
C#跨窗体程序调用方法的具体操作
2019-03-10
C#中创建Android项目
2019-03-10
统计学之变异系数与是非标志
2019-03-10
关于继承的一些基本知识
2019-03-10
抖音发布黄金时间段,抖音上热门最佳时间
2019-03-10
我的图床~
2019-03-10
Thymeleaf sec:authorize 标签不生效
2019-03-11