SpringBoot学习笔记(2):用Spring Security来保护你的应用

快速开始

本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程。

参考资料:

  SpringSecurity中文参考文档:点击进入

  IBM参考文档:点击进入

使用Maven进行构建

  首先,设置一个基本的构建脚本。在使用Spring构建应用程序时,您可以使用任何您喜欢的构建系统,但此处包含了使用Maven所需的代码。如果您不熟悉Maven,请参阅使用Maven构建Java项目。

  1. <!--添加Security依赖-->
  2. <dependency>
  3. <groupId>org.springframework.security</groupId>
  4. <artifactId>spring-security-test</artifactId>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-security</artifactId>
  10. </dependency>

创建一个不安全的应用

  在将安全性应用于Web应用程序之前,您需要一个Web应用程序来保护安全。本节中的步骤将引导您创建一个非常简单的Web应用程序。然后在下一节中使用Spring Security保护它。

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

  如您所见,这个简单的视图包含指向页面“/ hello”的链接。这在以下Thymeleaf模板中定义:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://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应用程序基于Spring MVC。因此,您需要配置Spring MVC并设置视图控制器以公开这些模板。这是在应用程序中配置Spring MVC的配置类。

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  3. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  4.  
  5. @Configuration
  6. public class MvcConfig implements WebMvcConfigurer {
  7.  
  8. @Override
  9. public void addViewControllers(ViewControllerRegistry registry) {
  10. registry.addViewController("/home").setViewName("home");
  11. registry.addViewController("/").setViewName("home");
  12. registry.addViewController("/hello").setViewName("hello");
  13. registry.addViewController("/login").setViewName("login");
  14. }
  15. }
  addViewControllers()方法(覆盖WebMvcConfigurer中的同名方法)添加了四个视图控制器。其中两个视图控制器引用名称为“home”的视图(在home.html中定义),另一个引用名为“hello”的视图(在hello.html中定义)。第四个视图控制器引用另一个名为“login”的视图。您将在下一部分中创建该视图。

  

设置Spring Security

  假设您要阻止未经授权的用户在“/ hello”查看问候语页面。就像现在一样,如果用户单击主页上的链接,他们会看到问候语,没有障碍阻止他们。您需要添加一个屏障,强制用户在查看该页面之前登录。

  您可以通过在应用程序中配置Spring Security来实现。如果Spring Security位于类路径上,则Spring Boot会自动使用“基本”身份验证来保护所有HTTP端点。但您可以进一步自定义安全设置。您需要做的第一件事是将Spring Security添加到类路径中。 

  我们已经在Maven构建部分加入了相应配置,此处不再赘余。

这是一个安全配置,可确保只有经过身份验证的用户才能看到秘密问候语:

  1. import org.springframework.context.annotation.Bean;
  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.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.core.userdetails.User;
  7. import org.springframework.security.core.userdetails.UserDetails;
  8. import org.springframework.security.core.userdetails.UserDetailsService;
  9. import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  10.  
  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")
  19. .permitAll()
  20. .anyRequest()
  21. .authenticated()
  22. .and()
  23. .formLogin()
  24. .permitAll()
  25. .and()
  26. .logout()
  27. .permitAll();
  28. }
  29. @Bean
  30. public UserDetailsService userDetailsService()
  31. {
  32. UserDetails user =
  33. User.withDefaultPasswordEncoder()
  34. .username("user")
  35. .password("password")
  36. .roles("USER")
  37. .build();
  38.  
  39. return new InMemoryUserDetailsManager(user);
  40. }
  41.  
  42. }

WebSecurityConfig类使用@EnableWebSecurity进行批注,以启用Spring Security的Web安全支持并提供Spring MVC集成。它还扩展了WebSecurityConfigurerAdapter并覆盖了其几个方法来设置Web安全配置的一些细节。

  configure(HttpSecurity)方法定义应该保护哪些URL路径,哪些不应该保护。具体而言,“/”和“/ home”路径配置为不需要任何身份验证。必须对所有其他路径进行身份验证。

  当用户成功登录时,它们将被重定向到先前请求的身份验证页面。 loginPage()指定了一个自定义的“/ login”页面,允许每个人查看它。

  对于userDetailsS​​ervice()方法,它使用单个用户设置内存用户存储。该用户被赋予用户名“user”,密码为“password”,角色为“USER”。

效果演示

  

  

  

编写简单的安全性配置

启用Web安全性功能最简单配置

  1. @Configuration
  2. @EnableWebSecurity //注解开启Spring Security的功能
  3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
  4.  
  5. }

说明:

  @EnableWebSecurity 将会启用Web安全功能,但他本身没有什么用。

  我们还需要配置一个实现了WebSecurityConfigurer的Bean。

指定Web安全的细节

  如果我们希望指定Web安全的细节,需要重载WebSecurityConfigureAdapter中的configure方法:

  

  默认的configure Filter链:

  1. protected void configure(HttpSecurity http) throws Exception {
  2. this.logger.debug("Using default configure(HttpSecurity).
                    If subclassed this will potentially override subclass configure(HttpSecurity).");
  3. ((HttpSecurity)((HttpSecurity)((AuthorizedUrl)http.
          authorizeRequests().
            anyRequest()).
              authenticated().
            and()).
          formLogin().
          and()).
          httpBasic();
  4. }

基于内存的用户存储

  1. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  2. //AuthenticationManagerBuilder提供了有多个方法来配置Security对认证的支持。
  3. auth.inMemoryAuthentication() //启用内存用户存储
  4. .withUser("user").password("password").roles("USER").and()
  5. .withUser("admin").password("password").roles("USER","ADMIN");
  6. }

基于数据库表进行认证

  1. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  2. //AuthenticationManagerBuilder提供了有多个方法来配置Security对认证的支持。
  3. auth.jdbcAuthentication()
  4. .dataSource(dataSource)
  5. .usersByUsernameQuery("SELECT username,password,enable FROM mybatis.user WHERE username=?")
  6. .authoritiesByUsernameQuery("SELECT username,role FROM mybatis.user WHERE username=?");
  7. }

细粒度控制访问

  1. protected void configure(HttpSecurity http) throws Exception {
  2. http
  3. .formLogin() //重写configure 需要添加formLogin来显示默认的登陆页面
  4. .loginPage("/login")//登录页面的访问路径
  5. .loginProcessingUrl("/check")//登录页面下表单提交的路径
  6. .failureUrl("/login")//登录失败后跳转的路径
  7. .defaultSuccessUrl("/show")//登录成功后默认跳转的路径
  8. .and()
  9. .authorizeRequests()
  10. .antMatchers("/index.do").authenticated()
  11. .anyRequest().permitAll(); //其他请求是允许的,不需要经过认证和权限
  12. }

基于注解的方式控制访问

简要介绍两种注解方式:

  • @Secured()注解

    • SpringBoot:

      1. @EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) //开启基于注解的安全验证
  • 基于表达式语法
    • Spring Security中定义了四个支持使用表达式的注解,分别是@PreAuthorize、@PostAuthorize、@PreFilter和@PostFilter。其中前两者可以用来在方法调用前或者调用后进行权限检查,后两者可以用来对集合类型的参数或者返回值进行过滤。
  1. public class HtmlController {
  2. @PreAuthorize("hasAuthority('ROLE_ADMIN')")
  3. @RequestMapping(value = "security.do")
  4. public String PreAuthorize()
  5. {
  6. return "@PreAuthorize:该注解用来确定一个方法是否应该被执行。" +
  7. "该注解后面跟着的是一个表达式,如果表达式的值为真,则该方法会被执行。" +
  8. "如 @PreAuthorize(\"hasRole('ROLE_USER')\")" +
  9. "就说明只有当前用户具有角色 ROLE_USER的时候才会执行。";
  10. }
  11.  
  12. @Secured("ROLE_ADMIN")
  13. @RequestMapping(value = "security1.do")
  14. public String Security()
  15. {
  16. return "@Secured是由Spring Security定义的用来支持方法权限控制的注解。" +
  17. "它的使用也是需要启用对应的支持才会生效的。" +
  18. "通过设置global-method-security元素的secured-annotations=”enabled”" +
  19. "可以启用Spring Security对使用@Secured注解标注的方法进行权限控制的支持,其值默认为disabled。";
  20. }
  21. }

配置自定义的用户服务

你好                                                                                                                                                                                                                                                                                                                                                                                                                              

添加验证码

SpringBoot学习笔记(2):引入Spring Security的更多相关文章

  1. springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)

    Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...

  2. SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群

    session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...

  3. SpringBoot学习笔记二之Spring整合Mybatis

    原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...

  4. SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用

    SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用 Spring Boot Admin是一个管理和监控Spring Boot应用程序的应用程序.本文参考文档: 官 ...

  5. SpringBoot学习笔记(4):添加自定义的过滤器

    SpringBoot:学习笔记(4)——添加自定义的过滤器 引入自定义过滤器 SpringBoot提供的前端控制器无法满足我们产品的需求时,我们需要添加自定义的过滤器. SpringBoot添加过滤器 ...

  6. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

  7. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

  8. SpringBoot学习笔记(13):日志框架

    SpringBoot学习笔记(13):日志框架——SL4J 快速开始 说明 SpringBoot底层选用SLF4J和LogBack日志框架. SLF4J的使用 SpringBoot的底层依赖关系 1. ...

  9. springboot 学习笔记(二)

    springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...

随机推荐

  1. 【BIEE】由于排序顺序不兼容,集合操作失败

    问题描述 使用BIEE数据透视表时,使用了UNION进行数据组合,但是在浏览结果时意外出错了,报错如下截图: 问题分析 原因暂时未知 问题解决 目前使用UNION进行聚合,只需要将UNION修改为UN ...

  2. 【Lucene】Apache Lucene全文检索引擎架构之构建索引2

    上一篇博文中已经对全文检索有了一定的了解,这篇文章主要来总结一下全文检索的第一步:构建索引.其实上一篇博文中的示例程序已经对构建索引写了一段程序了,而且那个程序还是挺完善的.不过从知识点的完整性来考虑 ...

  3. Android网络框架Volley

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  4. Android开发_Animation

    新建项目: http://www.cnblogs.com/hongten/gallery/image/112163.html 项目结构: http://www.cnblogs.com/hongten/ ...

  5. Cookie-Parser是怎样解析签名后的cookie的(同一时候对cookie和cookie-signature进行说明)

    第一步:我们来学习一下cookie-signature: var cookie=require('./index'); var val = cookie.sign('hello', 'tobiisco ...

  6. Excel中判断一个表中的某一列的数据在另一列中是否存在

      A B C D 1 10   3 有 2 6   e 无 3 3   6 有 判断c列的值在A列中是否存在(假定C列为需要判断列,A列为目标列) 在D1中输入以下公式,然后下拉公式即可 =IF(C ...

  7. 更改 Nginx 服务的默认用户

    为什么要更改 Nginx 服务的默认用户:就像更改 ssh 的默认 22 端口一样,增加安全性,Nginx 服务的默认用户是 nobody ,我们更改为 nginx 1.添加 nginx 用户 use ...

  8. PAT 1001. A+B Format(水题)

    #include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...

  9. ANDROID L——Material Design具体解释(主题和布局)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990).谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  10. freemarker在xml文件中遍历list数据

    delete   from pub_channelpackage   where channelcode = :channelcode   and channeltype = :channeltype ...