Spring学习日志之Spring Security配置
依赖引入
<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的类。这个操作会导致一个名为DelegatingFilterProxy的Filter被注册,它会拦截发往应用中的请求,并将请求委托给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();
}
}
自定义登录页面
如果不想做多余的配置,那么自定义的页面里:
form的action应该提交到/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配置的更多相关文章
- Spring学习日志之Spring MVC启动配置
对DispatcherServlet进行配置 Spring MVC的配置实际上就是对DispatcherServlet的配置 public class DispatcherServletConfig ...
- Spring学习日志之纯Java配置的MVC框架搭建
依赖引入 <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifa ...
- 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 ...
- <黑马新秀>Spring学习日志
# 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...
- Spring学习 6- Spring MVC (Spring MVC原理及配置详解)
百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Spring学习笔记之五----Spring MVC
Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- Spring学习1-初识Spring
一.简介 1.Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发. 2.为何要使用Spring? ...
随机推荐
- Overlapping rectangles判断两个矩形是否重叠的问题 C++
Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...
- Nodejs密集型CPU解决方案
首先说一下nodejs单线程的优势: 高性能,与php相比,避免了频繁创建切换线程的开销,执行更加迅速,资源占用小. 线程安全,不用担心同一变量被多线程读写,造成程序崩溃. 单线程的异步和非阻塞,其实 ...
- Extjs 取消backspace事件
Ext.getDoc().on('keydown',function(e){ if(e.getKey() == 8 && e.getTarget().type =='text' &am ...
- touch监听判断手指的上滑,下滑,左滑,右滑,事件监听
判断滑动的方向和距离,来实现一定的效果,比如返回上一页等等 <body> <script> $(function(){ //给body强制定义高度 var windowHeig ...
- Xampp配置本地域名及常见错误解决
本地域名配置 1.计算机-->C盘-->Windows-->System32-->drivers-->etc-->hosts 127.0.0.1 loc ...
- UNIX域协议(无名套接字)
关于什么是UNIX域套接字可以参考:http://www.cnblogs.com/xcywt/p/8185597.html这里主要介绍非命名的UNIX域套接字的用法.1.socketpair函数先看m ...
- 几个关于js数组方法reduce的经典片段
以下是个人在工作中收藏总结的一些关于javascript数组方法reduce的相关代码片段,后续遇到其他使用这个函数的场景,将会陆续添加,这里作为备忘. javascript数组那么多方法,为什么我要 ...
- SCOI 2010 序列操作
题目描述 lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b ...
- Python学习_08_函数式编程
在python中,函数名也是一个变量,代表对一个函数内容的引用,意味着可以作为参数传入到其他函数中,根据这个特性,发散出装饰器.闭包等概念,并涉及到变量作用域等问题. 函数 python中函数操作符为 ...
- python中的if __name__=='__main__': main()解析
python中我们会看到一段代码是这样的: if __name__=='__main__': main() 这段代码的什么意思,我们可以知道代码的意思是如果__name__=='__main__'为T ...