本文主要是介绍SpringSecurity6.0 如何通过JWTtoken进行认证授权,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《SpringSecurity6.0如何通过JWTtoken进行认证授权》:本文主要介绍SpringSecurity6.0通过JWTtoken进行认证授权的过程,本文给大家介绍的非常详细,感兴趣...
之前写过一个文章,从SpringSecurity 5.x升级到6.0,当时是为了配合公司的大版本升级做的,里面的各项配置都是前人留下来的,其实没有花时间进行研究SpringSecurity的工作机制。现在新东家有一个简单的系统要搭建,用户的认证授权流程也比较简单,通过用户/密码进行登录,登录后生成JWT token返回给前端,后续认证通过token进行,就把SpringSecurity重新捡了起来,搭建整个系统的安全认证框架。
项目依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- jwt token相关依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependencies>
项目后端整体还是通过Springboot来搭建,Springboot3.0中把JWT相关的依赖都整合到了spring-boot-starter-oauth2-resource-server
中,无需再单独指定
认证
首先我们先完成通过账号密码进行登录相关代码
@Configuration @EnableWebSecurity public class SecurityConfig { @Value("${jwt.public.key}") RSAPublicKey key; @Value("${jwt.private.key}") RSAPrivateKey priv; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { // @formatter:off http .authorizeHttpRequests((authorize) -> authorize .anyRequest().pythonauthenticated() ) .csrf((csrf) -> csrf.ignoringRequestMatchers("/token")) .httpBasic(Customizer.withDefaults()) .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt) .sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .exceptionHandling((exceptions) -> exceptions .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint()) .AccessDeniedHandler(new BearerTokenAccessDeniedHandler()) ); // @formatter:on return http.build(); } @Bean UserDetailsService users() { // @formatter:off return new InMemoryUserDetailsManager( User.withUsername("user") .password("{noop}password") .authorities("app") .build() ); // @formatter:on } @Bean JwtDecoder jwtDecoder() { return NimbusJwtDecoder.withPublicKey(this.key).build(); } @Bean JwtEncoder jwtEncoder() { JWK jwk = new RSAKey.Builder(this.key).privateKey(this.priv).build(); JWKSource<SecurityContext> jwks = new ImmutableJWKSet<>(new JWKSet(jwk)); return new NimbusJwtEncoder(jwks); } }
这里关注几个重点:
HttpSecurity#httpBasic
,这个方法表明通过基于HTTP Basic认证协议anyRequest().authenticated()
表明所有请求都需要经过认证UserDetailsService
,这里创建了一个仅存在于内存中的用户,用户名和密码是user/password,密码中添加的前缀{noop}和userDetailService的作用我们稍后再说oauth2ResourceServer
设置jwt token相关的配置,Spring推荐情况是配置一个第三方的校验服务,我们这里为了简化将相关的生成和校验都在本地进行。
UserDetailService
public interface UserDetailsService { /** * Locates the user based on the username. In the actual implementation, the search * may possibly be case sensitive, or case insensitive depending on how the * implementation instance is configured. In this case, the <code>UserDetails</code> * object that comes back may have a username that is of a different case than what * was actually requested.. * @param username the username identifying the user whose data is required. * @return a fully populated user record (never <code>null</code>) * @throws UsernameNotFoundException if the user could not be found or the user has no * GrantedAuthority */ UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; }
这个接口里只定义了一个方法,loadUserByUsername
在通过用户名/密码进行认证时,需要通过来判断用户是否存在,在生产中,我们可以根据自己的需要通过数据库等获取用户信息。
拿到用户信息之后,要怎么校验密码呢?SpringSecurity提供了另外一个接口PasswordEncoder
进行密码的编码和校验,
这里提供了非常多的实现方式,默认情况下Spring会加载DelegatingPasswordEncoder
,同时将其他的实现都包含进去,那在进行密码校验的时候要匹配哪一个Encoder呢,这里{noop}password
中的前缀就发挥作用了,{noop}
表明使用NoOpPasswordEncoder
进行处理,即不仅限任何编码处理,直接通过明文进行对比,这里当然不符合安全要求,在实际工作中我们根据需要直接指定一个Encoder即可
@Bean PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); }
生成JWT token
@RestController public class TokenController { @Autowired JwtEncoder encoder; @PostMapping("/token") public String token(Authentication authentication) { Instant now = Instant.now(); long expiry = 36000L; // @formatter:off StuJXRBring scope = authentication.getAuthorities().stream() .map(GrantedAuthority::getAuthority) .collect(Collectors.joining(" ")); JwtClaimsSet claims = JwtClaimsSet.builder() .issuer("self") .issuedAt(now) .expiresAt(now.plusSeconds(expiry)) .subject(authentication.getName()) .claim("scope", scope) .build(); // @formatter:on return this.encoder.encode(JwtEncoderParameters.from(claims)).getTokenValue(); } }
这里有一个小提示,我们在创建UserDetail的时候可以设置#authorities()
和#roles()
,但是最终都会设置到authorities中,这两个在当今的SpringSecurity中实际上是一个东西,所以我们在Authentication
中也只有getAuthorities()
这一个方法
进行测试
curl -XPOST user:password@localhost:8080编程/token
然后能够得到类似的返回
eyJhbGciOijsUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoidXNlciIsImV4cCI6MTYwNDA0MzA1MSwiaWF0IjoxNjA0MDA3MDUxfQ.yDF_JgSwl5sk21CF7AE1AYbYzRd5YYqe3MIgSwpgN0t2UqsjaaEDhmmICKizt-_0iZy8nkEpNnvgqv5bOHDhs7AXlYS1pg8dgPKuyfkhyVIKa3DhuGyb7tFjwJxHpr128BXf1Dbq-p7Njy46tbKsZhP5zGTjdXlqlAhR4Bl5Fxaxr7D0gdTvbVTlUp9DCyjs6l-pTBpsvHxShkjXJ0GHVpIZdB-c2e_K9PfTW5MDPcHekG9djnWPSEy-fRvKzTsyVFhdy-X3NXQWWkjFv9bNarV-bhXMLzqhujuaeXJGEqUZlkhBxTsqFr1N7XphpVcmhs3ECdjEyun2fUSge4BoC7budsQ
然后我们把token配置到环境变量中
export TOKEN=`curl -XPOST user:password@localhost:8080/token`
请求另外一个接口
curl -H "Authorization: Bearer $TOKEN" localhost:8080 && echo
Hello, user!
权限控制
在完成认证后,后续我们可以继续进行授权相关的校验工作,SpringSecurity提供两种授权校验的方式
- 基于http请求的方式,包括路径匹配、请求方法匹配等,
- 基于方法的控制,通过
@PreAuthorize
等注解,在方法上进行更细粒度的控制,我采用了这一种方式
@PreAuthorize("hasAuthority('SCOPE_ADMIN')")//JWT token解析后会加一个前缀'scope' @GetMapping("/admin") public String admin(Authentication authentication){ return authentication.getAuthorities().toString(); }
默认情况下,Authority
中的内容会比你生成token时多加一个前缀SCOPE_
,当然你也可以通过配置进行更改。
小结
这里简单介绍了一下认证和授权的配置,实际上SpringSecurity要远比这些要复杂的多,有更深入的需求可以参考官方文档或者源码
这里推荐一下自己的项目地址,已经把用户配置到h2数据库中
https://gitee.com/xiiiao/hello-spring-security
到此这篇关于SpringSecurity6.0 通过JWTtoken进行认证授权的文章就介绍到这了,更多相关SpringSecurity认证授权内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于SpringSecurity6.0 如何通过JWTtoken进行认证授权的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!