26-关于spring security的配置01

今天初步学完了spring security,学下来感觉虽复杂,但是框架本身考虑的确实够细致,够周到,后面一一说道

security的特点:

1:和spring无缝衔接

2:全面的权限控制

3:专为web开发

  3.1:旧版本b不能脱离web环境使用

  3.2:新版本都整个框架进行了分层抽取,分成了核心模块与web模块,单独引用核心模块就可以脱离web模块

  3.3:重量级框架

为什么要使用spring security?

前面提到,此框架是重量级框架,功能够全面,很多设置都是默认配置好的,当然也可以使用方法进行定制,

这就意味着什么mvc中的拦截器,异常捕获机制,登陆时的验证,密码加密这些操作,security都已经帮我们写好了

只需要拿来用即可!

想使用spring security最基本的配置:使用security web模块jar包

基于maven(便于版本控制)与 注解开发

pom文件中需要注入这三个依赖

<!-- SpringSecurity 对 Web 应用进行权限管理 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>
<!-- SpringSecurity 配置 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>
<!-- SpringSecurity 标签库 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>

web.xml中也需要配置security专门的filter
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
需要注意的是,security中使用的是filter而不是interceptor,意味着security管理的不仅仅
是springMVC中controller(hander)请求,还包括了weby应用中的所有请求,比如静态资源也会被拦截,
从而进行权限控制

特别注意<filter-name>springSecurityFilterChain</filter-name>中name的值必须为
springSecurityFilterChain,因为此name值对应了IOC中真正执行权限控制的20多个Filter,
只有这个名字才能加载到这些Filter;

因为使用的是用注解类代替xml
所以使用security需要创建一个类专门来设置框架的具体属性
此类需要继承webSecurityConfigurerAdapter类


@configuration :将当前类标记为配置类,此注解中包含了@component,所以会被当做组件进行扫描

@enableWbeSecurity: 启用web环境下的权限功能

效果:所有请求都会被spring security拦截,要求登陆后进行访问

    静态资源也会被拦截,要求登陆

    登陆失败有错误提示。

spring Security的基本配置大概就这么多,后面会写道如何一步步完善security

 



猜你喜欢

转载自www.cnblogs.com/myPrBB/p/12577880.html
今日推荐