基于该篇文章修改。目前官方文档并不完善,便做此记录。置方式来源于官方仓库issues oauth2-server模块pom添加thymeleaf依赖
登录页面 修改DefaultSecurityConfigorg.springframework.boot spring-boot-starter-thymeleaf
@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();
}
模板页面
登录页面
登录
添加控制器
@Slf4j
@Controller
public class Oauth2Controller {
@GetMapping("login")
public String login() {
return "login";
}
}
效果
授权页面
修改AuthorizationServerConfiguration
void defaultOAuth2AuthorizationServerConfigurer(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer = new OAuth2AuthorizationServerConfigurer<>();
RequestMatcher authorizationServerEndpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
//添加自定义授权页面
authorizationServerConfigurer.authorizationEndpoint(endpoint -> {
endpoint.consentPage("/oauth2/consent");
});
// 拦截 授权服务器相关的请求端点
http.requestMatcher(authorizationServerEndpointsMatcher)
.authorizeRequests().anyRequest().authenticated().and()
// 忽略掉相关端点的csrf
.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";
}
}
模板页面
Title
目录结构



