欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringSecurity6.0 如何通過JWTtoken進行認證授權

 更新時間:2025年04月09日 16:30:01   作者:nyzzht123  
這篇文章主要介紹了SpringSecurity6.0 通過JWTtoken進行認證授權的過程,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧

之前寫過一個文章,從SpringSecurity 5.x升級到6.0,當時是為了配合公司的大版本升級做的,里面的各項配置都是前人留下來的,其實沒有花時間進行研究SpringSecurity的工作機制?,F(xiàn)在新東家有一個簡單的系統(tǒng)要搭建,用戶的認證授權流程也比較簡單,通過用戶/密碼進行登錄,登錄后生成JWT token返回給前端,后續(xù)認證通過token進行,就把SpringSecurity重新?lián)炝似饋?,搭建整個系統(tǒng)的安全認證框架。

項目依賴

<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);
  }
}

這里關注幾個重點:

  • HttpSecurity#httpBasic,這個方法表明通過基于HTTP Basic認證協(xié)議
  • anyRequest().authenticated()表明所有請求都需要經過認證
  • UserDetailsService,這里創(chuàng)建了一個僅存在于內存中的用戶,用戶名和密碼是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在通過用戶名/密碼進行認證時,需要通過來判斷用戶是否存在,在生產中,我們可以根據(jù)自己的需要通過數(shù)據(jù)庫等獲取用戶信息。
拿到用戶信息之后,要怎么校驗密碼呢?SpringSecurity提供了另外一個接口PasswordEncoder進行密碼的編碼和校驗,

這里提供了非常多的實現(xiàn)方式,默認情況下Spring會加載DelegatingPasswordEncoder,同時將其他的實現(xiàn)都包含進去,那在進行密碼校驗的時候要匹配哪一個Encoder呢,這里{noop}password中的前綴就發(fā)揮作用了,{noop}表明使用NoOpPasswordEncoder進行處理,即不僅限任何編碼處理,直接通過明文進行對比,這里當然不符合安全要求,在實際工作中我們根據(jù)需要直接指定一個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();
  }
}

這里有一個小提示,我們在創(chuàng)建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配置到環(huán)境變量中

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

請求另外一個接口

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

權限控制

在完成認證后,后續(xù)我們可以繼續(xù)進行授權相關的校驗工作,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數(shù)據(jù)庫中
https://gitee.com/xiiiao/hello-spring-security

到此這篇關于SpringSecurity6.0 通過JWTtoken進行認證授權的文章就介紹到這了,更多相關SpringSecurity認證授權內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java關鍵字instanceof用法及實現(xiàn)策略

    Java關鍵字instanceof用法及實現(xiàn)策略

    instanceof 運算符是用來在運行時判斷對象是否是指定類及其父類的一個實例。這篇文章主要介紹了Java關鍵字instanceof用法解析,需要的朋友可以參考下
    2020-08-08
  • SpringBoot?Application核心注解詳解

    SpringBoot?Application核心注解詳解

    進入到@SpringBootApplication的源碼,可以看到里面組合了三個我們感興趣的注解@ComponentScan、@EnableAutoConfiguration、@SpringBootConfiguration,我們一一分析這三個注解
    2022-10-10
  • Java?stream流中peek用法簡單示例

    Java?stream流中peek用法簡單示例

    這篇文章主要給大家介紹了關于Java?stream流中peek用法的相關資料,Java Stream中的peek()方法也是用于查看每個元素,但不改變流的操作的方法,文中通過代碼介紹的需要的朋友可以參考下
    2023-12-12
  • Spring整合CXF webservice restful實例詳解

    Spring整合CXF webservice restful實例詳解

    這篇文章主要為大家詳細介紹了Spring整合CXF webservice restful的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Mybatis關聯(lián)查詢遇到的坑-無主鍵的關聯(lián)數(shù)據(jù)去重問題

    Mybatis關聯(lián)查詢遇到的坑-無主鍵的關聯(lián)數(shù)據(jù)去重問題

    這篇文章主要介紹了Mybatis關聯(lián)查詢遇到的坑-無主鍵的關聯(lián)數(shù)據(jù)去重問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Springboot 跨域配置無效及接口訪問報錯的解決方法

    Springboot 跨域配置無效及接口訪問報錯的解決方法

    這篇文章主要介紹了Springboot 跨域配置無效及接口訪問報錯的解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • spring cglib 與 jdk 動態(tài)代理

    spring cglib 與 jdk 動態(tài)代理

    本篇文章主要介紹了spring cglib與jdk動態(tài)代理的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05
  • SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏

    SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏

    這篇文章給大家介紹了三步實現(xiàn)SpringBoot全局日志記錄,整合Echarts實現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-03-03
  • Java實現(xiàn)將PPT轉為OFD過程詳解

    Java實現(xiàn)將PPT轉為OFD過程詳解

    本文將通過Java后端程序代碼展示如何實現(xiàn)將PPT幻燈片轉成OFD格式,文中的示例代碼講解詳細,對我們學習或工作有一定的幫助,需要的可以參考一下
    2022-01-01
  • 自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    這篇文章主要介紹了自定義@RequestBody注解如何獲取JSON數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論