Spring Security 自定义登录界面

川长思鸟来 2022-05-20 06:58 416阅读 0赞

一、Spring Security 简介

Spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。

一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。

用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。

用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。

#二、pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.xh</groupId>
  4. <artifactId>SpringSecurity-Demo</artifactId>
  5. <packaging>war</packaging>
  6. <version>1.0.0-SNAPSHOT</version>
  7. <name>SpringSecurity-Demo Maven Webapp</name>
  8. <url>http://maven.apache.org</url>
  9. <dependencyManagement>
  10. <dependencies>
  11. <!-- platform与cloud管理版本 -->
  12. <dependency>
  13. <groupId>io.spring.platform</groupId>
  14. <artifactId>platform-bom</artifactId>
  15. <version>Brussels-SR11</version>
  16. <!-- <version>Cairo-SR2</version> -->
  17. <type>pom</type>
  18. <scope>import</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-dependencies</artifactId>
  23. <!-- <version>Dalston.SR2</version> -->
  24. <version>Edgware.SR4</version>
  25. <!-- <version>Finchley.RELEASE</version> -->
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. </dependencies>
  30. </dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. </dependency>
  40. <!-- 与spring security 相关的jar包以及oauth2 -->
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-oauth2</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-data-redis</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-jdbc</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>mysql</groupId>
  55. <artifactId>mysql-connector-java</artifactId>
  56. </dependency>
  57. <!-- commons start -->
  58. <dependency>
  59. <groupId>commons-lang</groupId>
  60. <artifactId>commons-lang</artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>commons-collections</groupId>
  64. <artifactId>commons-collections</artifactId>
  65. </dependency>
  66. <dependency>
  67. <groupId>commons-beanutils</groupId>
  68. <artifactId>commons-beanutils</artifactId>
  69. </dependency>
  70. <!-- commons end -->
  71. </dependencies>
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.apache.maven.plugins</groupId>
  76. <artifactId>maven-compiler-plugin</artifactId>
  77. <version>2.3.2</version>
  78. <configuration>
  79. <!-- 源文件jdk版本 -->
  80. <source>1.8</source>
  81. <!-- 编译后jdk版本 -->
  82. <target>1.8</target>
  83. <encoding>UTF-8</encoding>
  84. </configuration>
  85. </plugin>
  86. </plugins>
  87. <finalName>SpringSecurity-Demo</finalName>
  88. </build>
  89. </project>

三、default password

在pom.xml中加入security的依赖后,启动springboot会自动启用security,当访问任何接口的时候会需要登录,例:
s

我访问 /user 请求就需要登录,其中默认用户名为:user,密码在启用springboot的时候,控制台打印出随机密码:
ps

输入对应密码就可以访问接口了:
i

去掉security验证:在application.properties中加入以下配置:

  1. # close spring security default configration. default username:user
  2. security.basic.enabled = false

但是去掉security拦截,与不加security没有什么区别。

四、自定义登录页面

在源文件src/main/resources 下面新建resources文件夹,在resources文件夹下面新建登录页面:myLogin.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录</title>
  6. </head>
  7. <body>
  8. <h3>表单登录</h3>
  9. <form action="/authentication/form" method="post">
  10. <table>
  11. <tr>
  12. <td>用户名:</td>
  13. <td><input type="text" name="username"></td>
  14. </tr>
  15. <tr>
  16. <td>密码:</td>
  17. <td><input type="password" name="password"></td>
  18. </tr>
  19. <tr>
  20. <td colspan="2"><button type="submit">登录</button></td>
  21. </tr>
  22. </table>
  23. </form>
  24. </body>
  25. </html>

新建class:SecurityConfig 继承 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter 重写 configure(HttpSecurity http) 方法 :

  1. package com.xh.sercurity.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. @Configuration
  6. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  7. @Override
  8. protected void configure(HttpSecurity http) throws Exception {
  9. // http.httpBasic()// httpBasic 登录
  10. http.formLogin()// 表单登录 来身份认证
  11. .loginPage("/myLogin.html")// 自定义登录页面
  12. .loginProcessingUrl("/authentication/form")// 自定义登录路径
  13. .and()
  14. .authorizeRequests()// 对请求授权
  15. // error 127.0.0.1 将您重定向的次数过多
  16. .antMatchers("/myLogin.html", "/authentication/require",
  17. "/authentication/form").permitAll()// 这些页面不需要身份认证,其他请求需要认证
  18. .anyRequest() // 任何请求
  19. .authenticated()//; // 都需要身份认证
  20. .and()
  21. .csrf().disable();// 禁用跨站攻击
  22. }
  23. }

那么在访问接口时需要验证就会自动跳转到自定义的登录界面。

发表评论

表情:
评论列表 (有 0 条评论,416人围观)

还没有评论,来说两句吧...

相关阅读