Security简单初级使用

一丶简介

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。

  它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoCDI(控制反转Inversion of Control

  DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,

  减少了为企业系统安全控制编写大量重复代码的工作。

二丶pom文件导入jar坐标

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

    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    </dependency>
三丶web.xml相关配置    <context      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring/spring-security.xml(对应项目中的路径)</param-value>
    </context-param>
    <listener>
     <listener-class>
   org.springframework.web.context.ContextLoaderListener
   </listener-class>
    </listener>

    <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.xml配置及常用标签简述
  1.xml关于Security约束头
   <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
       xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
          详细配置内容
    </beans:beans>
  2.<http pattern="放行资源" security="none">
      当该资源使用此标签时,secutiry="none"时表明该资源不受拦截,注意/*与/**的区别:/*指该目录下的本机资源,/**指该目录下的所有资源
  3.
<http use-expressions="false"></http> 在此标签内部设置拦截规则.设置页面拦截规则 声明此规则为false后可写为 access="ROLE_ADMIN",默认为true,
      写法为 access="hasRole('ROLE_ADMIN')"为是否启用SPEL表达式,当访问规则复杂时比如匹配IP地址访问可使用该表达式.
  4.在3内部的标签.
<intercept-url pattern="/*" access="ROLE_ADMIN"/>当前用户必须有ROLE_ADMIN的角色才能访问根目录及根目录下的所有资源
  5.在3内部的标签.<form-login/>与<logout/>开启表单登录页与注销页.在form-login中有属性login-page=""使用指定登录页面,default-target-url为登录成功
            后跳转的页面.always-use-default-target设置为true时,每次登录成功后都跳转的default-target-url指定的页面
  6.在3内部的标签.
<csrf disabled="true"/><!--CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF
              ,是一种对网站的恶意利用。不设置会出现403错误-->
  7.在3内部的标签.当系统中使用了框架页,应有以下配置才可使用.作用设置框架页的策略为SAMEORIGIN
         <headers>
         <frame-options policy="SAMEORIGIN"/>
       </headers>
  8.认证管理器.(此为在配置文件中写死的配置)
        <authentication-manager>
        <authentication-provider>
        <user-service>
        <user name="admin" password="123123" authorities="ROLE_ADMIN"/>
         </user-service>
        </authentication-provider>
        </authentication-manager>
  9认证管理器.使用如下配置,需要将认证类声明为bean,让认证全体提供user-service-ref去引用.
      <authentication-manager alias="authenticationManager">
      <authentication-provider user-service-ref="userDetailService">

      </authentication-provider>
      </authentication-manager>
  10.搭配9,认证类的编写.
      a.编写UserDetailsServiceImpl类去实现UserDetailsService接口.
      b.返回值为UserDetails.我们返回其实现类User,当返回值为null时,表示验证不通过.
      c.User有一个构造方法需要三个参数.new User(
String username, String password, Collection<? extends GrantedAuthority> authorities)
      d.
GrantedAuthority权限,User的第三个参数为一个权限集合.而GrantedAuthority为接口,所以该集合中我们放入的是 他的一个实现类,在此可以放一个简单的
   
      SimpleGrantedAuthority,他的构造方法需要一个字符串,代表权限,比如"ROLE_ADMIN"或者"ROLE_SELLER",要以ROLE开头.
      e.其他参数username页面传过来的,可以根据其去数据匹配看有没有该用户,然后再匹配密码是否正确.
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/hanpi/p/11743436.html