oauth2-server模块pom添加thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
登录页面
修改DefaultSecurityConfig
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http.formLogin(form ->
form.loginPage("/login")
.loginProcessingUrl("/login")
)
.authorizeRequests(requests ->
requests.antMatchers("/login").permitAll()
.anyRequest().authenticated()
);
return http.build();
}
模板页面
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<h3>登录</h3>
<form th:action="@{/login}" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">登录</button>
</td>
</tr>
</table>
</form>
</body>
</html>
添加控制器
@Slf4j
@Controller
public class Oauth2Controller {
@GetMapping("login")
public String login() {
return "login";
}
}
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/22eaf68d09c4420b83ffb16ff3418e66.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bKB5pyIIOODoSDkuI3lj4rml7blhYnplb8=,size_11,color_FFFFFF,t_70,g_se,x_16)
授权页面
修改AuthorizationServerConfiguration
void defaultOAuth2AuthorizationServerConfigurer(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer = new OAuth2AuthorizationServerConfigurer<>();
RequestMatcher authorizationServerEndpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
authorizationServerConfigurer.authorizationEndpoint(endpoint -> {
endpoint.consentPage("/oauth2/consent");
});
http.requestMatcher(authorizationServerEndpointsMatcher)
.authorizeRequests().anyRequest().authenticated().and()
.csrf(csrf -> csrf.ignoringRequestMatchers(authorizationServerEndpointsMatcher))
.apply(authorizationServerConfigurer);
}
添加控制器
@Slf4j
@Controller
public class Oauth2Controller {
@GetMapping("login")
public String login() {
return "login";
}
@RequestMapping("/oauth2/consent")
public String consent(@RequestParam String scope, @RequestParam String client_id, @RequestParam String state, Authentication authentication, Model model) {
log.info("/oauth2/consent------>scope:{} client_id:{} state:{} authentication:{}",scope,client_id,state,authentication);
model.addAttribute("scopes", scope.split(" "));
model.addAttribute("clientId", client_id);
model.addAttribute("state", state);
return "consent";
}
}
模板页面
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="@{/oauth2/authorize}">
<input type="hidden" name="client_id" th:value="${clientId}"/>
<input type="hidden" name="state" th:value="${state}"/>
<div th:each="scope : ${scopes}">
<input type="checkbox" name="scope" th:value="${scope}" th:id="${scope}" th:text="${scope}"/><br/>
</div>
<button type="submit">同意</button>
</form>
</body>
</html>
目录结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/c0c7d937bcb44dd68fc50032ec6ab78b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bKB5pyIIOODoSDkuI3lj4rml7blhYnplb8=,size_13,color_FFFFFF,t_70,g_se,x_16)