官网https://spring.io/guides/gs/securing-web/

无情的翻译官。。。。。。

1.1 依赖包导入

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>securing-web</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>securing-web</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-security</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.security</groupId>
  34. <artifactId>spring-security-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. <exclusions>
  42. <exclusion>
  43. <groupId>org.junit.vintage</groupId>
  44. <artifactId>junit-vintage-engine</artifactId>
  45. </exclusion>
  46. </exclusions>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

里面引入了测试要用到的web模块,thymeleaf引擎模块,以及咱们的security模块

2.1 没有安全机制下的一个web

测试的项目有两个页面home.html和hello.html

src/main/resources/templates/home.html:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  3. <head>
  4. <title>Spring Security Example</title>
  5. </head>
  6. <body>
  7. <h1>Welcome!</h1>
  8. <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
  9. </body>
  10. </html>

home页面提交到一个/hello请求,会返回一个hello.html:

src/main/resources/templates/hello.html:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
  3. xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <h1>Hello world!</h1>
  9. </body>
  10. </html>

这个web项目依赖于springmvc,我们可以自定义一下视图解析器:

src/main/java/com/example/securingweb/MvcConfig.java:

  1. package com.example.securingweb;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class MvcConfig implements WebMvcConfigurer {
  7. public void addViewControllers(ViewControllerRegistry registry) {
  8. registry.addViewController("/home").setViewName("home");
  9. registry.addViewController("/").setViewName("home");
  10. registry.addViewController("/hello").setViewName("hello");
  11. registry.addViewController("/login").setViewName("login");
  12. }
  13. }

这种无安全机制的web任何人都可以通过localhost:8080/hello 直接访问到hello.html.

假设我们要防止那些没有经过授权的用户进入到hello.html页面,就需要用到我们的security模块。

3.1 做一个安全机制的web

在引入了我们的security包的情况下,我们需要自定义下我们的安全规则;

src/main/java/com/example/securingweb/WebSecurityConfig.java:

  1. package com.example.securingweb;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.core.userdetails.User;
  8. import org.springframework.security.core.userdetails.UserDetails;
  9. import org.springframework.security.core.userdetails.UserDetailsService;
  10. import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  11. @Configuration
  12. @EnableWebSecurity
  13. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http
  17. .authorizeRequests()
  18. .antMatchers("/", "/home").permitAll()
  19. .anyRequest().authenticated()
  20. .and()
  21. .formLogin()
  22. .loginPage("/login")
  23. .permitAll()
  24. .and()
  25. .logout()
  26. .permitAll();
  27. }
  28. @Bean
  29. @Override
  30. public UserDetailsService userDetailsService() {
  31. UserDetails user =
  32. User.withDefaultPasswordEncoder()
  33. .username("user")
  34. .password("password")
  35. .roles("USER")
  36. .build();
  37. return new InMemoryUserDetailsManager(user);
  38. }
  39. }
  • @EnableWebSecurity:用于开启springSecurity模块功能和提供与springmvc集成的功能
  • WebSecurityConfigurerAdapter:自定义的配置都要继承这个类
  • configure(HttpSecurity):定义了哪些http请求需要被安全加密,哪些可以被放行通过。这里"/","home"这两个路径不需要任何授权请求,也就是说任何人都可以访问。这里注意链式写法用.and()衔接。
  • formLogin()和logout()这两个一般都是单独定制,对于formLogin()我们把它转向到我们自己写的login页面,否则SpringSecurity在拦截到一个非法请求的时候会返回你一个自带的login页面。当然对于这两个页面我们都是对放行的。
  • userDetailsService() :定义了一个内存对象,这里注意withDefaultPasswordEncoder(),高版本的SpringSecurity已经不允许明码形式的密码,必须对密码进行加密再传送。

src/main/resources/templates/login.html:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
  3. xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Spring Security Example </title>
  6. </head>
  7. <body>
  8. <div th:if="${param.error}">
  9. Invalid username and password.
  10. </div>
  11. <div th:if="${param.logout}">
  12. You have been logged out.
  13. </div>
  14. <form th:action="@{/login}" method="post">
  15. <div><label> User Name : <input type="text" name="username"/> </label></div>
  16. <div><label> Password: <input type="password" name="password"/> </label></div>
  17. <div><input type="submit" value="Sign In"/></div>
  18. </form>
  19. </body>
  20. </html>

src/main/resources/templates/hello.html:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
  3. xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
  9. <form th:action="@{/logout}" method="post">
  10. <input type="submit" value="Sign Out"/>
  11. </form>
  12. </body>
  13. </html>

springSecurity使用的更多相关文章

  1. spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置

    spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  2. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(前言)

    一直希望能够搭建一个完整的,基础Web框架,方便日后接一些外快的时候,能够省时省力,终于花了一周的时间,把这个东西搞定了.特此写下此博客,一来是纪念,二来是希望能够为别人提供方便.顺带说一下,恩,组合 ...

  3. 【JavaWeb】SSM+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(六)

    Showings 我个人的项目,当前不断地在更新. 我希望做成一个好项目,同时,也是在锻炼自己的技术. 在项目中发现问题,学习知识,是比较可取的一条路子. 这样学习到的知识,虽然分散,但是都很实用,而 ...

  4. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(五)

    SpringSecurity(2) 好久没有写了,之前只写了一半,我是一边开发一边写Blog一边上班,所以真心没有那么多时间来维护Blog,项目已经开发到编写逻辑及页面部分了,框架基本上已经搭建好不会 ...

  5. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(四)

    SpringSecurity(1) 其实啊,这部分我是最不想写的,因为最麻烦的也是这部分,真的是非常非常的麻烦.关于SpringSecurity的配置,让我折腾了好半天,网上的配置方式一大把,但总有一 ...

  6. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)

    Spring+MyBatis 首先要搭建的是Spring+MyBatis的整合框架,毕竟Spring是整个Web框架的核心部位,而数据库操作是一切测试的基础嘛. 目录结构 ━java ┣ contro ...

  7. SSO单点登录Spring-Security & CAS使用手册

    1.1概述 1.1.1单点登录介绍 单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要登录一次就可 ...

  8. spring4.2.3+mybatis+spring-security配置文件

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  9. springmvc+spring-security+mybatis +redis +solar框架抽取

    参考文章:Spring MVC 3 深入总结: 第二章 Spring MVC入门 —— 跟开涛学SpringMVC 参考博客:http://www.cnblogs.com/liukemng/categ ...

  10. 安全框架 SpringSecurity 和 Shiro 对比

    突然再次很想理一下权限的事,但是实在不知道实际情况选哪个框架好,现在整理下网上的资料,做一下对比. 1.Spring-security 对spring 结合较好,如果项目用的springmvc ,使用 ...

随机推荐

  1. Java实现 蓝桥杯VIP 算法提高 任意年月日历输出

    算法提高 任意年月日历输出 时间限制:1.0s 内存限制:512.0MB 已知2007年1月1日为星期一. 设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印. 为 ...

  2. jsp页面用DBHelper实现简单的登陆验证

    首先我们需要写一个简单的登陆页面login.jsp,然后用from表单提交给index.jsp页面.在index.jsp页面通过DBHelper连接数据库判断账号和密码,如果密码正确则显示登陆成功. ...

  3. Zabbix+Orabbix监控oracle数据库表空间

    Orabbix 是设计用来为 zabbix 监控 Oracle 数据库的插件,它提供多层次的监控,包括可用性和服务器性能指标. 它提供了从众多 oracle 实例采集数据的有效机制,进而提供此信息的监 ...

  4. 一次性搞懂 PHP 中面向对象的所有知识点。

    OOP是什么? OOP是面向对象编程,面向对象编程是一种计算机编程架构. OOP的基本原则是计算机程序是由单个能起到子程序作用的单元或对象组合而成. 基本概念: 类:定义了事务的抽象特点.包含了数据的 ...

  5. 【优雅写代码系统】springboot+mybatis+pagehelper+mybatisplus+druid教你如何优雅写代码

    目录 spring基本搭建 整合mybatis pom配置 mybatis配置 设置数据源 设置sqlsessionfactory 设置扫描 设置开启事务 资源放行 测试 结果 思考&& ...

  6. logrotate 如何执行日志按照大小切分

    说在最先的话,logrotate要设置按照文件大小来配置日志切分,需要通过三个东西. 1.配置logrotate 的配置文件 命名未任意文件,在启动的时候指定,例如/etc/weblog.conf 参 ...

  7. 使用nodejs的puppeteer库爬取瓜子二手车网站

    const puppeteer = require('puppeteer'); (async () => { const fs = require("fs"); const ...

  8. pip 安装使用国内镜像

    pip国内的一些镜像 阿里云 https://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...

  9. 分享一个我自己做的 Excel 万年历

    下载链接在此. 纯 Excel 公式实现,带农历,可自定义节日.配色. 带有紧凑日历和记事日历两种日历,均可直接 Ctrl+P 打印,且打印时不带有顶部的控制栏.

  10. # 详细了解HTML5中的form表单

    4.规范使用 <form>元素用于组织所有表单部件(也称为控件或字段),负责告诉浏览器把数据提交到哪里,方法是在action属性中提供一个URL.加入你只是想在客户端使用JavaScrip ...