spring security+oauth2自定义授权页

在我们微信登录时或者qq登录时 都会有以下页面

这个就是我们所说的授权页 但是你别无选择 必须同意才能继续操作

而spring security + oauth2 也提供了授权页 如下图 

如图 比较难看  红色框里面的数据来自oauth_client_details表的scope字段

配合以下配置使用

自定义授权页面

如果说你嫌弃这个授权页面太难看了 想要自己制作一个 可以通过以下配置

AuthorizationServerConfig

修改资源中心的核心配置类 AuthorizationServerConfig 如果不懂请看上文 配置意思就是重写/custom/confirm_access接口 这里我们就更改接口名称 还是用原来接口

扫描二维码关注公众号,回复: 12471616 查看本文章

getAccessConfirmation

getAccessConfirmation是一个thymeleaf接口 跳转到前台

@Controller
@SessionAttributes("authorizationRequest")
public class GrantController {

    /**
     * 自定义授权页面thymeleaf
     * 
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/custom/confirm_access")
    public ModelAndView getAccessConfirmation(Map<String, Object> model, HttpServletRequest request) throws Exception {
        AuthorizationRequest authorizationRequest = (AuthorizationRequest)model.get("authorizationRequest");
        ModelAndView view = new ModelAndView();
        view.setViewName("/grant");
        view.addObject("clientId", authorizationRequest.getClientId());
        view.addObject("scope", authorizationRequest.getScope());
        return view;
    }
}

@SessionAttributes 注解只用作用在 类 上,作用是将指定的 Model 的键值对保存在 session 中。可以让其他请求共用 session 中的键值对。

grant.html

因为本人不会前台 所以这里参考了其他的样式  如果有其他需求 可以更改样式

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>授权</title>
</head>
<style>
    html {
        padding: 0px;
        margin: 0px;
    }
    .title {
        background-color: #E9686B;
        height: 50px;
        padding-left: 20%;
        padding-right: 20%;
        color: white;
        line-height: 50px;
        font-size: 18px;
    }
    .title-left {
        float: right;
    }
    .title-right {
        float: left;
    }
    .title-left a {
        color: white;
    }
    .container {
        clear: both;
        text-align: center;
    }
    .btn {
        width: 350px;
        height: 35px;
        line-height: 35px;
        cursor: pointer;
        margin-top: 20px;
        border-radius: 3px;
        background-color: #E9686B;
        color: white;
        border: none;
        font-size: 15px;
    }
</style>
<body style="margin: 0px">
<div class="title">
    <div class="title-right">Spring Security 授权</div>
    <div class="title-left">
        <a href="#help">帮助</a>
    </div>
</div>
<div class="container">
    <h3 th:text="${clientId}+' 请求授权,该应用将获取你的以下信息'"></h3>
    <form method="post" action="/authentication/oauth/authorize">
        <input type="hidden" name="user_oauth_approval" value="true">
        <ul style="list-style-type: none">
            <li th:each="s:${scope}">
                <div class="form-group">
                    <a th:text="${s}"></a>:
                    <input type="radio" th:name="'scope.'+${s}" value="true">Approve(授权)
                    <input type="radio" th:name="'scope.'+${s}" value="false" checked="">Deny(拒绝)
                </div>
            </li>
        </ul>
        授权后表明你已同意 <a href="#boot" style="color: #E9686B">服务协议</a><br>
        <button class="btn" type="submit" name="authorize" th:value="Authorize"> 同意/授权</button>
    </form>
</div>
</body>
</html>

修改后的样式

猜你喜欢

转载自blog.csdn.net/qq_20143059/article/details/113770570