栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Authorization Server自定义登录与授权页面

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Authorization Server自定义登录与授权页面

基于该篇文章修改。目前官方文档并不完善,便做此记录。置方式来源于官方仓库issues oauth2-server模块pom添加thymeleaf依赖

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
登录页面 修改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();
    }
模板页面



    
    登录页面


登录

用户名:
密码:
添加控制器
@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



目录结构

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号