java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringSecurity认证授权

SpringSecurity6.0 如何通过JWTtoken进行认证授权

作者:nyzzht123

这篇文章主要介绍了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().authenticated()
        )
        .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);
  }
}

这里关注几个重点:

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
    String 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-p7Njy46tbKsZhP5zGTjdXlqlAhR4Bl5Fxaxr7D0gdTVBVTlUp9DCy6l-pTBpsvHxShkjXJ0GHVpIZdB-c2e_K9PfTW5MDPcHekG9djnWPSEy-fRvKzTsyVFhdy-X3NXQWWkjFv9bNarV-bhxMlzqhujuaeXJGEqUZlkhBxTsqFr1N7XVcmhs3ECdjEyun2fUSge4BoC7budsQ

然后我们把token配置到环境变量中

export TOKEN=`curl -XPOST user:password@localhost:8080/token`

请求另外一个接口

curl -H "Authorization: Bearer $TOKEN" localhost:8080 && echo
Hello, user!

权限控制

在完成认证后,后续我们可以继续进行授权相关的校验工作,SpringSecurity提供两种授权校验的方式

  @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认证授权内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文