依赖引入

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency> <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>

启用Spring Security

  • 首先创建一个继承AbstractSecurityWebApplicationInitializer的类。这个操作会导致一个名为DelegatingFilterProxyFilter被注册,它会拦截发往应用中的请求,并将请求委托给ID为SpringSecurityFilterChain的bean
public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer
{
}
  • 再创建一个配置类
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
}

至此Spring Security就被启用了,目前所有的请求都会被拦截

配置

WebSecurityConfigurerAdapter中有三个名为configure的方法提供重载,三个方法的描述如下:

方法 描述
configure(HttpSecurity) 配置拦截模式
configure(AuthenticationManagerBuilder) 配置用户信息
configure(WebSecurity) 配置Spring Security的Filter链

配置用户信息

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication() // 基于内存的用户存储
.withUser("admin")
.password("password")
.roles("USER", "ADMIN");
}

配置拦截路径

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/info").authenticated()
.antMatchers(HttpMethod.GET, "/health").hasAnyAuthority("ADMIN")
.anyRequest().permitAll();
}
}

启用HTTPS

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.anyRequest().permitAll()
.and()
.requiresChannel()
.antMatchers("/bankInfo").requiresSecure() // enable HTTPS
.antMatchers("/").requiresInsecure(); // disable HTTPS
}
}

禁用CSRF

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
}

登录与注销

  • 在重写configure(HttpSecurity)之前会有一个默认的登录页面,需要登录时会自动跳转到这个位于/login下的页面,但一旦重写此方法后就会失去这个简单的登录页面。

启用默认登录页

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.anyRequest().permitAll()
.and()
.formLogin();
}
}

自定义登录页面

如果不想做多余的配置,那么自定义的页面里:

  • formaction应该提交到/login
  • 包含username的输入域且name属性为username
  • 包含password的输入域且name属性为password

指定URL

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.anyRequest().permitAll()
.and()
.formLogin().loginPage("/login");
}
}

指定自定义页面

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}

ViewControllerRegistry可以用来直接转发请求到一个视图而无需编写控制器类。

注销

  • 如果没有启用CSRF,直接访问\logout就可实现登出
  • 如果启用CSRF,用POST方法访问\logout并带上CSRF Token

Spring学习日志之Spring Security配置的更多相关文章

  1. Spring学习日志之Spring MVC启动配置

    对DispatcherServlet进行配置 Spring MVC的配置实际上就是对DispatcherServlet的配置 public class DispatcherServletConfig ...

  2. Spring学习日志之纯Java配置的MVC框架搭建

    依赖引入 <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifa ...

  3. Spring学习1:Spring基本特性

    http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...

  4. <黑马新秀>Spring学习日志

    # 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...

  5. Spring学习 6- Spring MVC (Spring MVC原理及配置详解)

    百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...

  6. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  7. Spring学习笔记之五----Spring MVC

    Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...

  8. Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式

     本节主要内容:    1. 给MessageBean注入参数值    2. 测试Spring自动组件扫描方式    3. 如何控制ExampleBean实例化方式    4. 使用注解方式重构Jdb ...

  9. Spring学习1-初识Spring

    一.简介   1.Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发.  2.为何要使用Spring?   ...

随机推荐

  1. UVA 10559 Blocks

    题目大意:有一串带颜色的方块,每次可以消掉颜色相同的一段,得到size^2的分数,问最多能得到多少分数.n≤200. 给这题状态跪下来. 显然的区间DP,但设f[i][j]是不够的. 考虑到之前做过的 ...

  2. lesson - 3 笔记 ls /alias /ldd /cd /pwd /环境变量 / 目录

    一.ls 命令 作用:用来显示目录列表. 语法: ls  (选项) (参数) 选项: -a: 显示所有档案以及目录(ls内定将档案或目录名称为“./..”的视为隐藏) -A: 显示除隐藏文件“./.. ...

  3. Android短视频SDK转码实践

    一. 前言 一些涉及的基本概念: 转码:一般指多媒体文件格式的转换,比如分辨率.码率.封装格式等: 解复用(demux):从某种封装中分离出视频track和音频track,然后交给后续模块进行处理: ...

  4. mac安全权限解决

    如果有以下提示的,并不是文件损坏了,而是macOS Sierra新系统取消了安装本地程序的功能.   解决办法如下: 1.首先打开终端(找不到哪里打开终端 command+空格 搜索 "终端 ...

  5. javamelody 使用

    javamelody 扩展API如何获取监控参数 https://github.com/javamelody/javamelody/wiki/ExternalAPI#json

  6. mysql也有complex view merging 这个特性(5.6 , 5.7)

    出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...

  7. IDLE3.6.3 Mac版不支持中文输入解决办法

    最近安装了IDLE 3.6.3版本 但是在IDLE中要输入中文注释时发现虽然输入法切换到了中文,但输入的还是英文.然后我在IDLE外试了下,输入中文没问题,于是就确认应该是IDLE的问题. 网上查询到 ...

  8. ZJOI 2015 诸神眷顾的幻想乡

    题目描述 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看.幽香当然也非常 ...

  9. Java核心技术(Java白皮书)卷Ⅰ 第一章 Java程序设计概述

    第1章 Java程序设计概述1.1 Java程序设计平台 具有令人赏心悦目的语法和易于理解的语言,与其他许多优秀语言一样,Java满足这些要求. 可移植性 垃圾收集 提供大型的库  如果想要有奇特的绘 ...

  10. [Micropython]发光二极管制作炫彩跑马灯

       先甩锅 做完后才发现最后一个灯坏了,就坏了一个灯也不好意思去找淘宝店家,大家视频凑合着看把.不过并不影响实验效果.因为这个发光二极管白天不是很明显 晚上炫彩效果就能出来了.本次实验用的是8个灯珠 ...