欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 【springsecurity oauth2授权中心】自定义登录页和授权确认页 P2

【springsecurity oauth2授权中心】自定义登录页和授权确认页 P2

2025/12/12 19:32:48 来源:https://blog.csdn.net/liygheart/article/details/147411837  浏览:    关键词:【springsecurity oauth2授权中心】自定义登录页和授权确认页 P2

上一篇跑通了springsecurity oauth2的授权中心授权流程,这篇来将内置的登录页和授权确认页自定义一下

引入Thymeleaf

在模块authorization-server下的pom.xml里引入模板引擎

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

修改配置

AuthorizationServerConfig 类里的 authorizationServerSecurityFilterChain() 中的授权页修改

// 之前
http.getConfigurer(OAuth2AuthorizationServerConfigurer.class).oidc(Customizer.withDefaults());
//修改后
http.exceptionHandling(exceptions ->exceptions.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))).oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()));

defaultSecurityFilterChain() 方法修改

//之前
http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated()).formLogin(Customizer.withDefaults());
//修改后
http.authorizeHttpRequests(authorize -> authorize.requestMatchers("/login", "/static/**", "/css/**").permitAll().anyRequest().authenticated()).formLogin(form -> form.loginPage("/login").permitAll());

添加页面

resources/templates里添加 login.html, consent.html

login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>自定义登录页</title><link rel="stylesheet" href="/css/auth.css"/>
</head>
<body>
<h1>请登录</h1>
<form th:action="@{/login}" method="post"><input type="text" name="username" placeholder="用户名" required/><input type="password" name="password" placeholder="密码" required/><button type="submit">登录</button>
</form>
</body>
</html>

consent.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>授权请求</title><link rel="stylesheet" href="/css/auth.css"/>
</head>
<body>
<h1>授权申请</h1>
<p>应用 <strong th:text="${clientId}">客户端ID</strong> 请求以下权限:</p>
<ul><li th:each="scope : ${scopes}"><span th:text="${scopeNames?.get(scope) ?: scope}">权限范围</span></li>
</ul>
<form th:action="@{/oauth2/authorize}" method="post"><input type="hidden" name="client_id" th:value="${clientId}"/><input type="hidden" name="state" th:value="${state}"/><input type="hidden" name="scope" th:value="${#strings.arrayJoin(scopes, ' ')}"/><button type="submit" name="user_oauth_approval" value="true">同意</button><button type="submit" name="user_oauth_approval" value="false">拒绝</button>
</form>
</body>
</html>

创建一个css resources/static/css/auth.css
auth.css

/* static/css/auth.css */
body {font-family: Arial, serif;background-color: aliceblue;
}.auth-form {max-width: 400px;margin: 0 auto;
}

添加controller

import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;import java.util.HashMap;
import java.util.Map;@Controller
public class AuthorizationController {private final RegisteredClientRepository registeredClientRepository;public AuthorizationController(RegisteredClientRepository registeredClientRepository) {this.registeredClientRepository = registeredClientRepository;}@GetMapping("/login")public String login() {return "login";}@GetMapping("/oauth2/consent")public String consentPage(@RequestParam("client_id") String clientId,@RequestParam("scope") String scope,@RequestParam("state") String state,Model model) {// 创建 scope 到友好名称的映射Map<String, String> scopeNames = new HashMap<>();scopeNames.put("user", "读取用户信息");model.addAttribute("clientId", clientId);model.addAttribute("scopes", scope.split(" "));model.addAttribute("scopeNames", scopeNames);model.addAttribute("state", state);return "consent";}
}

测试

启动授权中心和客户端应用两个服务

打开浏览器访问:http://localhost:9000/oauth2/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8081/login/oauth2/code/client&scope=user

跳转到登录页
在这里插入图片描述
输入用户名和密码登录成功跳转到授权确认页
在这里插入图片描述
点击同意,带着code回调到客户端应用提供的回调地址上
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词