-
Notifications
You must be signed in to change notification settings - Fork 1
satoken鉴权 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TinyAlmond
wants to merge
1
commit into
InvolutionHell:suxiaoyu
Choose a base branch
from
TinyAlmond:suxiaoyu
base: suxiaoyu
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
satoken鉴权 #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,8 @@ | |
| <artifactId>spring-boot-starter-jdbc</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- spring security & oauth2 --> | ||
| <!-- spring security & oauth2 (Temporarily Commented Out for JustAuth Migration) --> | ||
| <!-- | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-security</artifactId> | ||
|
|
@@ -74,6 +75,25 @@ | |
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> | ||
| </dependency> | ||
| --> | ||
|
|
||
| <!-- JustAuth Core & Http Client --> | ||
| <dependency> | ||
| <groupId>me.zhyd.oauth</groupId> | ||
| <artifactId>JustAuth</artifactId> | ||
| <version>1.16.6</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>cn.hutool</groupId> | ||
| <artifactId>hutool-http</artifactId> | ||
| <version>5.8.25</version> | ||
| </dependency> | ||
| <!-- 添加 JustAuth 必须的 json 解析器 (Hutool 的依赖) --> | ||
| <dependency> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里请使用默认的jackson |
||
| <groupId>cn.hutool</groupId> | ||
| <artifactId>hutool-json</artifactId> | ||
| <version>5.8.25</version> | ||
| </dependency> | ||
|
|
||
| <!-- postgresql --> | ||
| <dependency> | ||
|
|
@@ -82,6 +102,13 @@ | |
| <scope>runtime</scope> | ||
| </dependency> | ||
|
|
||
| <!-- Sa-Token 权限认证 --> | ||
| <dependency> | ||
| <groupId>cn.dev33</groupId> | ||
| <artifactId>sa-token-spring-boot3-starter</artifactId> | ||
| <version>1.39.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- <!– redis –>--> | ||
| <!-- <dependency>--> | ||
| <!-- <groupId>org.springframework.boot</groupId>--> | ||
|
|
||
26 changes: 26 additions & 0 deletions
26
src/main/java/com/involutionhell/backend/common/config/SaTokenConfigure.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.involutionhell.backend.common.config; | ||
|
|
||
| import cn.dev33.satoken.interceptor.SaInterceptor; | ||
| import cn.dev33.satoken.router.SaRouter; | ||
| import cn.dev33.satoken.stp.StpUtil; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class SaTokenConfigure implements WebMvcConfigurer { | ||
|
|
||
| // 注册 SaToken 拦截器 | ||
| @Override | ||
| public void addInterceptors(InterceptorRegistry registry) { | ||
| // 注册 SaToken 拦截器,定义详细认证规则 | ||
| registry.addInterceptor(new SaInterceptor(handler -> { | ||
| // 拦截规则配置 | ||
| SaRouter | ||
| .match("/**") // 拦截所有路由 | ||
| .notMatch("/api/auth/login") // 排除登录接口 | ||
| .notMatch("/api/auth/register") // 排除注册接口 // 排除 Spring Boot 默认错误界面 | ||
| .check(r -> StpUtil.checkLogin()); // 校验是否登录,如果未登录,这里会抛出 NotLoginException | ||
| })).addPathPatterns("/**"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 53 additions & 51 deletions
104
src/main/java/com/involutionhell/backend/usercenter/config/SecurityConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,53 @@ | ||
| package com.involutionhell.backend.usercenter.config; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.oauth2.jwt.JwtDecoder; | ||
| import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
|
|
||
| import javax.crypto.spec.SecretKeySpec; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import static org.springframework.security.config.Customizer.withDefaults; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| @EnableMethodSecurity | ||
| public class SecurityConfig { | ||
|
|
||
| @Value("${jwt.secret-key}") | ||
| private String secretKey; | ||
|
|
||
| @Bean | ||
| public JwtDecoder jwtDecoder() { | ||
| // 使用 HmacSHA256 算法生成 SecretKeySpec | ||
| SecretKeySpec keySpec = new SecretKeySpec( | ||
| secretKey.getBytes(StandardCharsets.UTF_8), | ||
| "HmacSHA256" | ||
| ); | ||
| return NimbusJwtDecoder.withSecretKey(keySpec).build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .csrf(AbstractHttpConfigurer::disable) | ||
| .authorizeHttpRequests(authorize -> authorize | ||
| .requestMatchers("/api/auth/login", "/actuator/**", "/public/**").permitAll() | ||
| .anyRequest().authenticated() | ||
| ) | ||
| .oauth2Login(withDefaults()) // 启用 OAuth2 登录支持 | ||
| .oauth2Client(withDefaults()) // 启用 OAuth2 Client 支持 | ||
| .oauth2ResourceServer(oauth2 -> oauth2.jwt(withDefaults())); // 启用 JWT 校验 (Resource Server) | ||
|
|
||
| return http.build(); | ||
| } | ||
| } | ||
| // Temporarily commented out for JustAuth Migration | ||
|
|
||
| // package com.involutionhell.backend.usercenter.config; | ||
|
|
||
| // import org.springframework.beans.factory.annotation.Value; | ||
| // import org.springframework.context.annotation.Bean; | ||
| // import org.springframework.context.annotation.Configuration; | ||
| // import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
| // import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| // import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| // import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| // import org.springframework.security.oauth2.jwt.JwtDecoder; | ||
| // import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; | ||
| // import org.springframework.security.web.SecurityFilterChain; | ||
|
|
||
| // import javax.crypto.spec.SecretKeySpec; | ||
| // import java.nio.charset.StandardCharsets; | ||
|
|
||
| // import static org.springframework.security.config.Customizer.withDefaults; | ||
|
|
||
| // @Configuration | ||
| // @EnableWebSecurity | ||
| // @EnableMethodSecurity | ||
| // public class SecurityConfig { | ||
| // | ||
| // @Value("${jwt.secret-key}") | ||
| // private String secretKey; | ||
|
|
||
| // @Bean | ||
| // public JwtDecoder jwtDecoder() { | ||
| // // 使用 HmacSHA256 算法生成 SecretKeySpec | ||
| // SecretKeySpec keySpec = new SecretKeySpec( | ||
| // secretKey.getBytes(StandardCharsets.UTF_8), | ||
| // "HmacSHA256" | ||
| // ); | ||
| // return NimbusJwtDecoder.withSecretKey(keySpec).build(); | ||
| // } | ||
|
|
||
| // @Bean | ||
| // public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
| // http | ||
| // .csrf(AbstractHttpConfigurer::disable) | ||
| // .authorizeHttpRequests(authorize -> authorize | ||
| // .requestMatchers("/api/auth/login", "/actuator/**", "/public/**").permitAll() | ||
| // .anyRequest().authenticated() | ||
| // ) | ||
| // .oauth2Login(withDefaults()) // 启用 OAuth2 登录支持 | ||
| // .oauth2Client(withDefaults()) // 启用 OAuth2 Client 支持 | ||
| // .oauth2ResourceServer(oauth2 -> oauth2.jwt(withDefaults())); // 启用 JWT 校验 (Resource Server) | ||
|
|
||
| // return http.build(); | ||
| // } | ||
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里请使用默认的http请求工具