Skip to content

Commit 3a24a54

Browse files
committed
✨ Spring Security Oauth2
1 parent 7ad8dd1 commit 3a24a54

File tree

6 files changed

+157
-2
lines changed

6 files changed

+157
-2
lines changed

twelvet-auth/src/main/java/com/twelvet/auth/config/AuthorizationServerConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity h
8484
DefaultSecurityFilterChain securityFilterChain = http.authorizeHttpRequests(authorizeRequests -> {
8585
// 自定义接口、端点暴露
8686
authorizeRequests
87-
.requestMatchers("/api/token/*", "/token/**", "/actuator/**", "/assets/**", "/error", "/v3/api-docs")
87+
.requestMatchers("/api/token/*", "/token/**", "/actuator/**", "/assets/**", "/error", "/v3/api-docs",
88+
"/login/oauth2/**")
8889
.permitAll();
8990
authorizeRequests.anyRequest().authenticated();
9091
}).build();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* package com.twelvet.auth.endpoint;
3+
*
4+
* import com.twelvet.framework.core.application.domain.AjaxResult; import
5+
* io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag;
6+
* import
7+
* org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
8+
* import org.springframework.web.bind.annotation.GetMapping; import
9+
* org.springframework.web.bind.annotation.RestController;
10+
*
11+
*/
12+
/**
13+
* @author twelvet
14+
* @WebSite twelvet.cn
15+
* @Description: 测试第三方登录
16+
*//*
17+
*
18+
* @Tag(description = "IndexController", name = "首页显示内容Test")
19+
*
20+
* @RestController public class IndexController {
21+
*
22+
* @Operation(summary = "Test获取当前登录用户信息OAuth2")
23+
*
24+
* @GetMapping("/") public AjaxResult callback(OAuth2AuthenticationToken
25+
* oAuth2AuthenticationToken) { return AjaxResult.success(oAuth2AuthenticationToken);
26+
* }
27+
*
28+
* }
29+
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.twelvet.auth.controller;
2+
3+
import com.twelvet.auth.service.Oauth2AuthService;
4+
import com.twelvet.framework.core.application.controller.TWTController;
5+
import com.twelvet.framework.core.application.domain.JsonResult;
6+
import com.twelvet.framework.security.annotation.AuthIgnore;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.tags.Tag;
9+
import me.zhyd.oauth.model.AuthCallback;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
/**
17+
* <p>
18+
* 第三方登录
19+
* <p>
20+
*
21+
* @since 2025/1/16
22+
*/
23+
@AuthIgnore(value = false)
24+
@Tag(description = "Oauth2AuthController", name = "OAuth2登录优化管理")
25+
@RestController
26+
@RequestMapping("/login/oauth2")
27+
public class Oauth2AuthController extends TWTController {
28+
29+
@Autowired
30+
private Oauth2AuthService oauth2AuthService;
31+
32+
/**
33+
* 获取登录地址
34+
* @return
35+
*/
36+
@Operation(summary = "获取登录地址")
37+
@GetMapping
38+
public JsonResult<String> getAuthorize() {
39+
return JsonResult.success(oauth2AuthService.getAuthorize());
40+
}
41+
42+
@Operation(summary = "测试回调")
43+
@GetMapping("/code/{oauthCode}")
44+
public JsonResult<Object> login(@PathVariable String oauthCode, AuthCallback callback) {
45+
// 设置代理
46+
System.setProperty("http.proxyHost", "127.0.0.1");
47+
System.setProperty("http.proxyPort", "7890");
48+
System.setProperty("https.proxyHost", "127.0.0.1");
49+
System.setProperty("https.proxyPort", "7890");
50+
return JsonResult.success(oauth2AuthService.login(callback));
51+
}
52+
53+
}

twelvet-auth/src/main/java/com/twelvet/auth/endpoint/api/TokenEndpointApi.java renamed to twelvet-auth/src/main/java/com/twelvet/auth/controller/api/TokenEndpointApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.twelvet.auth.endpoint.api;
1+
package com.twelvet.auth.controller.api;
22

33
import cn.hutool.core.date.DatePattern;
44
import cn.hutool.core.date.TemporalAccessorUtil;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.twelvet.auth.service;
2+
3+
import me.zhyd.oauth.model.AuthCallback;
4+
5+
/**
6+
* <p>
7+
* 第三方登录
8+
* <p>
9+
*
10+
* @since 2025/1/16
11+
*/
12+
public interface Oauth2AuthService {
13+
14+
/**
15+
* 获取第三方授权地址
16+
* @return String
17+
*/
18+
String getAuthorize();
19+
20+
/**
21+
* 测试回调
22+
* @param callback
23+
* @return
24+
*/
25+
Object login(AuthCallback callback);
26+
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.twelvet.auth.service.impl;
2+
3+
import com.twelvet.auth.service.Oauth2AuthService;
4+
import jakarta.servlet.http.HttpServletResponse;
5+
import me.zhyd.oauth.config.AuthConfig;
6+
import me.zhyd.oauth.model.AuthCallback;
7+
import me.zhyd.oauth.request.AuthGithubRequest;
8+
import me.zhyd.oauth.request.AuthRequest;
9+
import me.zhyd.oauth.utils.AuthStateUtils;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
13+
/**
14+
* <p>
15+
*
16+
* <p>
17+
*
18+
* @since 2025/1/16
19+
*/
20+
@Service
21+
public class Oauth2AuthServiceImpl implements Oauth2AuthService {
22+
23+
/**
24+
* 第三方授权地址
25+
* @return 第三方授权地址
26+
*/
27+
public String getAuthorize() {
28+
AuthRequest authRequest = getAuthRequest();
29+
return authRequest.authorize(AuthStateUtils.createState());
30+
}
31+
32+
public Object login(AuthCallback callback) {
33+
AuthRequest authRequest = getAuthRequest();
34+
return authRequest.login(callback);
35+
}
36+
37+
private AuthRequest getAuthRequest() {
38+
return new AuthGithubRequest(AuthConfig.builder()
39+
.clientId("ff")
40+
.clientSecret("a82624")
41+
.redirectUri("http://localhost:8080/auth/login/oauth2/code/github")
42+
.build());
43+
}
44+
45+
}

0 commit comments

Comments
 (0)