当前位置:   article > 正文

Spring boot+Security OAuth2 自定义登录和授权页面_endpoints.pathmapping作用自定义确认授权页面

endpoints.pathmapping作用自定义确认授权页面

在学习了Spring Security oAuth2.0框架的基础知识,以及动手搭建简单的认证服务器和资源服务器的基础上,我们开始实现自定义登陆和授权界面的开发。

 

在实际的项目开发中,我们需要根据需要自定义oAuth2.0的登陆和授权界面。以下是具体的开发步骤:

第一步:首先需要引入thymeleaf 模板引擎(Spring boot框架推荐使用thymeleaf开发前端界面)

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

第二步:在spring boot工程的application.yml配置文件中配置thymeleaf

  1. spring:
  2. application:
  3. name: oauth2-server
  4. datasource:
  5. type: com.zaxxer.hikari.HikariDataSource
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. jdbc-url: jdbc:mysql://10.111.31.28:3306/oauth2?useUnicode=true&characterEncoding=utf-8&useSSL=false
  8. username: root
  9. password: root
  10. hikari:
  11. minimum-idle: 5
  12. idle-timeout: 600000
  13. maximum-pool-size: 10
  14. auto-commit: true
  15. pool-name: MyHikariCP
  16. max-lifetime: 1800000
  17. connection-timeout: 30000
  18. connection-test-query: SELECT 1
  19. thymeleaf:
  20. prefix: classpath:/views/
  21. suffix: .html
  22. cache: false
  23. mvc:
  24. throw-exception-if-no-handler-found: true
  25. server:
  26. port: 8080
  27. mybatis:
  28. type-aliases-package: com.funtl.oauth2.server.domain
  29. mapper-locations: classpath:mapper/*.xml

第三步:登陆界面,授权界面重新设计

自定义登录页面肯定要有自己的页面,先从页面入手,在resources 目录下新建views 目录,在此目录下新建base-login.html 文件如下:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录</title>
  6. </head>
  7. <style>
  8. .login-container {
  9. margin: 50px;
  10. width: 100%;
  11. }
  12. .form-container {
  13. margin: 0px auto;
  14. width: 50%;
  15. text-align: center;
  16. box-shadow: 1px 1px 10px #888888;
  17. height: 300px;
  18. padding: 5px;
  19. }
  20. input {
  21. margin-top: 10px;
  22. width: 350px;
  23. height: 30px;
  24. border-radius: 3px;
  25. border: 1px #E9686B solid;
  26. padding-left: 2px;
  27. }
  28. .btn {
  29. width: 350px;
  30. height: 35px;
  31. line-height: 35px;
  32. cursor: pointer;
  33. margin-top: 20px;
  34. border-radius: 3px;
  35. background-color: #E9686B;
  36. color: white;
  37. border: none;
  38. font-size: 15px;
  39. }
  40. .title{
  41. margin-top: 5px;
  42. font-size: 18px;
  43. color: #E9686B;
  44. }
  45. </style>
  46. <body>
  47. <div class="login-container">
  48. <div class="form-container">
  49. <p class="title">用户登录</p>
  50. <form name="loginForm" method="post" th:action="${loginProcessUrl}">
  51. <input type="text" name="username" placeholder="用户名"/>
  52. <br>
  53. <input type="password" name="password" placeholder="密码"/>
  54. <br>
  55. <button type="submit" class="btn">&nbsp;&nbsp;</button>
  56. </form>
  57. <p style="color: red" th:if="${param.error}">用户名或密码错误</p>
  58. </div>
  59. </div>
  60. </body>
  61. </html>

在views文件夹下新建base-grant.html 授权页面文件,如下所示

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>授权</title>
  6. </head>
  7. <style>
  8. html{
  9. padding: 0px;
  10. margin: 0px;
  11. }
  12. .title {
  13. background-color: #E9686B;
  14. height: 50px;
  15. padding-left: 20%;
  16. padding-right: 20%;
  17. color: white;
  18. line-height: 50px;
  19. font-size: 18px;
  20. }
  21. .title-left{
  22. float: right;
  23. }
  24. .title-right{
  25. float: left;
  26. }
  27. .title-left a{
  28. color: white;
  29. }
  30. .container{
  31. clear: both;
  32. text-align: center;
  33. }
  34. .btn {
  35. width: 350px;
  36. height: 35px;
  37. line-height: 35px;
  38. cursor: pointer;
  39. margin-top: 20px;
  40. border-radius: 3px;
  41. background-color: #E9686B;
  42. color: white;
  43. border: none;
  44. font-size: 15px;
  45. }
  46. </style>
  47. <body style="margin: 0px">
  48. <div class="title">
  49. <div class="title-right">OAUTH-BOOT 授权</div>
  50. <div class="title-left">
  51. <a href="#help">帮助</a>
  52. </div>
  53. </div>
  54. <div class="container">
  55. <h3 th:text="${clientId}+' 请求授权,该应用将获取你的以下信息'"></h3>
  56. <p>昵称,头像和性别</p>
  57. 授权后表明你已同意 <a href="#boot" style="color: #E9686B">OAUTH-BOOT 服务协议</a>
  58. <form method="post" action="/oauth/authorize">
  59. <input type="hidden" name="user_oauth_approval" value="true">
  60. <input type="hidden" name="_csrf" th:value="${_csrf.getToken()}"/>
  61. <div th:each="item:${scopes}">
  62. <input type="radio" th:name="'scope.'+${item}" value="true" hidden="hidden" checked="checked"/>
  63. </div>
  64. <button class="btn" type="submit"> 同意/授权</button>
  65. </form>
  66. </div>
  67. </body>
  68. </html>

第四步:定义Controller

登陆界面Controller

  1. package com.funtl.oauth2.server.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.security.oauth2.provider.AuthorizationRequest;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. @Controller
  8. public class BaseMainController {
  9. @GetMapping("/auth/login")
  10. public String loginPage(Model model){
  11. model.addAttribute("loginProcessUrl","/auth/authorize");
  12. return "base-login";
  13. }
  14. }

WebSecurity 配置

授权前的用户认证有Security 提供,将自定义的登录页面配置进去

  1. package com.funtl.oauth2.server.config;
  2. import com.funtl.oauth2.server.config.service.UserDetailsServiceImpl;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  6. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  13. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  15. @Configuration
  16. @EnableWebSecurity
  17. @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
  18. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  19. @Bean
  20. public BCryptPasswordEncoder passwordEncoder() {
  21. // 设置默认的加密方式
  22. return new BCryptPasswordEncoder();
  23. }
  24. @Bean
  25. @Override
  26. public UserDetailsService userDetailsService() {
  27. return new UserDetailsServiceImpl();
  28. }
  29. @Override
  30. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  31. // 使用自定义认证与授权
  32. auth.userDetailsService(userDetailsService());
  33. }
  34. @Override
  35. public void configure(WebSecurity web) throws Exception {
  36. // 将 check_token 暴露出去,否则资源服务器访问时报 403 错误
  37. web.ignoring().antMatchers("/oauth/check_token");
  38. }
  39. @Override
  40. protected void configure(HttpSecurity http) throws Exception {
  41. http
  42. // 必须配置,不然OAuth2的http配置不生效----不明觉厉
  43. .requestMatchers()
  44. .antMatchers("/auth/login", "/auth/authorize","/oauth/authorize")
  45. .and()
  46. .authorizeRequests()
  47. // 自定义页面或处理url是,如果不配置全局允许,浏览器会提示服务器将页面转发多次
  48. .antMatchers("/auth/login", "/auth/authorize")
  49. .permitAll()
  50. .anyRequest()
  51. .authenticated();
  52. // 表单登录
  53. http.formLogin()
  54. // 登录页面
  55. .loginPage("/auth/login")
  56. // 登录处理url
  57. .loginProcessingUrl("/auth/authorize");
  58. http.httpBasic().disable();
  59. }
  60. }

到这里已经完成了自定义登录页的功能,接下来继续说自定义授权页面

自定义授权页面

授权Controller

  1. package com.funtl.oauth2.server.controller;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.springframework.security.oauth2.provider.AuthorizationRequest;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.SessionAttributes;
  8. import org.springframework.web.servlet.ModelAndView;
  9. @Controller
  10. @SessionAttributes("authorizationRequest")
  11. public class BootGrantController {
  12. //@RequestMapping("/oauth/confirm_access")
  13. @RequestMapping("/custom/confirm_access")
  14. public ModelAndView getAccessConfirmation(Map<String, Object> model, HttpServletRequest request) throws Exception {
  15. AuthorizationRequest authorizationRequest = (AuthorizationRequest) model.get("authorizationRequest");
  16. ModelAndView view = new ModelAndView();
  17. view.setViewName("base-grant");
  18. view.addObject("clientId", authorizationRequest.getClientId());
  19. view.addObject("scopes",authorizationRequest.getScope());
  20. return view;
  21. }
  22. }

 在认证服务配置文件AuthorizationServerConfiguration中添加如下配置

  1. @Override
  2. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  3. 。。。。。。。。。。。。。
  4. // 最后一个参数为替换之后授权页面的url
  5. endpoints.pathMapping("/oauth/confirm_access","/custom/confirm_access");
  6. }

最后即可开始测试:

效果图如下

å¨è¿éæå¥å¾çæè¿°

 

源码地址 

https://download.csdn.net/download/u013310119/11275096

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/84236
推荐阅读
相关标签
  

闽ICP备14008679号