Skip to content

Commit bceec18

Browse files
committed
feat: 优化授权站点登录与个人主页
1 parent 7e320e9 commit bceec18

11 files changed

Lines changed: 1059 additions & 55 deletions

File tree

pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<!-- 编译 -->
2424
<maven.compiler.source>17</maven.compiler.source>
2525
<maven.compiler.target>17</maven.compiler.target>
26+
<maven-compiler-plugin.version>3.14.1</maven-compiler-plugin.version>
27+
<lombok.version>1.18.42</lombok.version>
2628
<!-- 依赖 -->
2729
<bootstrap.version>5.3.3</bootstrap.version>
2830
</properties>
@@ -75,6 +77,12 @@
7577
<groupId>org.springframework.boot</groupId>
7678
<artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
7779
</dependency>
80+
<!-- Lombok annotations are used by entity, config and handler classes. -->
81+
<dependency>
82+
<groupId>org.projectlombok</groupId>
83+
<artifactId>lombok</artifactId>
84+
<scope>provided</scope>
85+
</dependency>
7886
</dependencies>
7987

8088
<build>
@@ -88,6 +96,20 @@
8896
<artifactId>maven-compiler-plugin</artifactId>
8997
<configuration>
9098
<parameters>true</parameters>
99+
<annotationProcessorPaths>
100+
<path>
101+
<groupId>org.projectlombok</groupId>
102+
<artifactId>lombok</artifactId>
103+
<version>${lombok.version}</version>
104+
</path>
105+
</annotationProcessorPaths>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-surefire-plugin</artifactId>
111+
<configuration>
112+
<argLine>-Duser.home=${project.build.directory}/test-home</argLine>
91113
</configuration>
92114
</plugin>
93115
<!-- git commit插件 -->

src/main/java/io/github/opensabre/authorization/config/WebSecurityConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity httpSecurity)
3939
// url安全配置
4040
httpSecurity.authorizeHttpRequests((authorizeHttpRequests) ->
4141
authorizeHttpRequests
42-
.requestMatchers("/doc.html", "/v3/**", "/webjars/**", "/assets/**", "/login")
42+
.requestMatchers("/doc.html", "/v3/**", "/webjars/**", "/assets/**", "favicon.svg", "/login")
4343
.permitAll()
44-
.requestMatchers("/client**", "/oauth2/activate*", "/oauth2/consent", "/")
44+
.requestMatchers("/client**", "/oauth2/activate*", "/oauth2/consent", "/", "/profile")
4545
.authenticated());
4646
// 表单登录处理从授权服务器过滤器链
4747
httpSecurity
48-
.formLogin(formLogin -> formLogin.loginPage("/login"))
48+
.formLogin(formLogin -> formLogin
49+
.loginPage("/login")
50+
.defaultSuccessUrl("/profile", false))
4951
.userDetailsService(userDetailsService);
5052
// 添加BearerTokenAuthenticationFilter,将认证服务当做一个资源服务,解析请求头中的token
5153
httpSecurity.oauth2ResourceServer((resourceServer) -> resourceServer
5254
.jwt(Customizer.withDefaults()));
5355
return httpSecurity.build();
5456
}
55-
}
57+
}

src/main/java/io/github/opensabre/authorization/rest/AuthorizationController.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import cn.hutool.core.util.StrUtil;
44
import io.github.opensabre.authorization.entity.ScopeWithDescription;
5+
import io.github.opensabre.authorization.entity.User;
6+
import io.github.opensabre.authorization.service.IUserService;
57
import jakarta.annotation.Resource;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.security.core.GrantedAuthority;
610
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
711
import org.springframework.security.oauth2.core.oidc.OidcScopes;
812
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent;
@@ -32,6 +36,9 @@ public class AuthorizationController {
3236
@Resource
3337
private OAuth2AuthorizationConsentService authorizationConsentService;
3438

39+
@Resource
40+
private IUserService userService;
41+
3542
/**
3643
* 登陆页面
3744
*
@@ -42,6 +49,28 @@ public String login() {
4249
return "login";
4350
}
4451

52+
/**
53+
* 用户个人主页
54+
*
55+
* @param principal 认证信息
56+
* @param model 页面model
57+
* @return 用户个人主页
58+
*/
59+
@GetMapping({"/", "/profile"})
60+
public String profile(Authentication principal, Model model) {
61+
User user = userService.getByUniqueId(principal.getName());
62+
String displayName = user != null && StrUtil.isNotBlank(user.getName()) ? user.getName() : principal.getName();
63+
Set<String> authorities = principal.getAuthorities().stream()
64+
.map(GrantedAuthority::getAuthority)
65+
.collect(Collectors.toSet());
66+
model.addAttribute("user", user);
67+
model.addAttribute("principalName", principal.getName());
68+
model.addAttribute("displayName", displayName);
69+
model.addAttribute("avatarText", StrUtil.subPre(displayName, 1).toUpperCase());
70+
model.addAttribute("authorities", authorities);
71+
return "profile";
72+
}
73+
4574
/**
4675
* 授权确认页面
4776
*
@@ -116,4 +145,4 @@ public String activated() {
116145
private static Set<ScopeWithDescription> withDescription(Set<String> scopes) {
117146
return scopes.stream().map(ScopeWithDescription::new).collect(Collectors.toSet());
118147
}
119-
}
148+
}

0 commit comments

Comments
 (0)