SpringSecurity整合JWT的使用示例
Spring Security是一个强大的安全性框架,它提供了许多强大的功能来保护应用程序,而JWT(JSON Web Token)是一种用于在网络环境中传递声明的开放标准。
整合Spring Security和JWT,可以使我们的应用程序更加安全和高效。下面是整合步骤:
添加Spring Security和JWT的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency>
配置Spring Security
在Spring的配置类中,我们需要设置一些安全配置,包括:
- 配置安全规则
- 配置JWT过滤器
- 配置认证管理器
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { private static final String[] AUTH_WHITELIST = { "/swagger-resources/**", "/swagger-ui.html", "/v2/api-docs", "/webjars/**" }; @Autowired private JwtFilter jwtFilter; @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity http) throws Exception { http .cors().and().csrf().disable() .authorizeRequests() .antMatchers(AUTH_WHITELIST).permitAll() .antMatchers("/api/authenticate").permitAll() .anyRequest().authenticated() .and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class); } @Bean(BeanIds.AUTHENTICATION_MANAGER) public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } }
配置JWT
@Configuration public class JwtConfig { @Value("${jwt.secret}") private String secret; @Value("${jwt.expiration}") private long expiration; @Bean public JwtEncoder jwtEncoder() { return new JwtEncoder(secret, expiration); } @Bean public JwtDecoder jwtDecoder() { return new JwtDecoder(secret); } }
实现自定义UserDetailsService
我们需要提供一个实现了UserDetailsService接口的自定义类,用于从数据库中获取用户信息。
@Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found with username: " + username); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))); } }
实现JwtEncoder和JwtDecoder
我们需要提供一个JwtEncoder和JwtDecoder类,用于创建和验证JWT。
public class JwtEncoder { private final String secret; private final long expiration; public JwtEncoder(String secret, long expiration) { this.secret = secret; this.expiration = expiration; } public String createToken(UserDetails userDetails) { Map<String, Object> claims = new HashMap<>(); claims.put("sub", userDetails.getUsername()); claims.put("iat", new Date()); claims.put("exp", new Date(System.currentTimeMillis() + expiration)); return Jwts.builder() .setClaims(claims) .signWith(SignatureAlgorithm.HS512, secret) .compact(); } } public class JwtDecoder { private final String secret; public JwtDecoder(String secret) { this.secret = secret; } public String getUsernameFromToken(String token) { return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody().getSubject(); } public boolean validateToken(String token) { try { Jwts.parser().setSigningKey(secret).parseClaimsJws(token); return true; } catch (SignatureException e) { LOGGER.error("Invalid JWT signature - {}", e.getMessage()); } catch (MalformedJwtException e) { LOGGER.error("Invalid JWT token - {}", e.getMessage()); } catch (ExpiredJwtException e) { LOGGER.error("Expired JWT token - {}", e.getMessage()); } catch (UnsupportedJwtException e) { LOGGER.error("Unsupported JWT token - {}", e.getMessage()); } catch (IllegalArgumentException e) { LOGGER.error("JWT claims string is empty - {}", e.getMessage()); } return false; } }
实现JWT过滤器
我们需要提供一个JwtFilter类,用于过滤JWT。
@Component public class JwtFilter extends OncePerRequestFilter { @Autowired private JwtDecoder jwtDecoder; @Autowired private UserDetailsService userDetailsService; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String header = request.getHeader("Authorization"); if (StringUtils.isBlank(header) || !header.startsWith("Bearer ")) { chain.doFilter(request, response); return; } String token = header.replace("Bearer ", ""); if (jwtDecoder.validateToken(token)) { String username = jwtDecoder.getUsernameFromToken(token); UserDetails userDetails = userDetailsService.loadUserByUsername(username); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); } }
至此,我们已经成功地整合了Spring Security和JWT。更多相关SpringSecurity整合JWT内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
- SpringSecurity+Redis+Jwt实现用户认证授权
- springboot+springsecurity+mybatis+JWT+Redis 实现前后端离实战教程
- SpringBoot3.0+SpringSecurity6.0+JWT的实现
- SpringBoot整合SpringSecurity和JWT和Redis实现统一鉴权认证
- SpringBoot+SpringSecurity+jwt实现验证
- SpringSecurity详解整合JWT实现全过程
- mall整合SpringSecurity及JWT认证授权实战下
- mall整合SpringSecurity及JWT实现认证授权实战
- Java SpringSecurity+JWT实现登录认证
- springSecurity+jwt使用小结
相关文章
SpringBoot + Disruptor实现特快高并发处理及使用Disruptor高速实现队列的过程
Disruptor是一个开源的Java框架,它被设计用于在生产者—消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS)和尽量低的延迟,这篇文章主要介绍了SpringBoot + Disruptor 实现特快高并发处理,使用Disruptor高速实现队列,需要的朋友可以参考下2023-11-11详解Spring Boot 使用Spring security 集成CAS
本篇文章主要介绍了详解Spring Boot 使用Spring security 集成CAS,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05java后端+前端使用WebSocket实现消息推送的详细流程
后端向前端推送消息就需要长连接,首先想到的就是websocket,下面这篇文章主要给大家介绍了关于java后端+前端使用WebSocket实现消息推送的详细流程,需要的朋友可以参考下2022-10-10
最新评论