SpringSecurity框架1(初识)

SpringSecurity框架1(初识)

- 简介:

SpringSecurity框架是一个安全框架
Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业软件项目。人们使用Spring Security有很多种原因,不过通常吸引他们的是在J2EE Servlet规范或EJB规范中找不到典型企业应用场景的解决方案。特别要指出的是他们不能再WAR 或 EAR 级别进行移植。这样,如果你更换服务器环境,就要,在新的目标环境进行大量的工作,对你的应用系统进行重新配 置安全。使用Spring Security 解决了这些问题,也为你提供很多有用的,完全可以指定的其他安全特性。安全包括两个主要操作。

• “认证”,是为用户建立一个他所声明的主体。主题一般式指用户,设备或可以在你系 统中执行动作的其他系统。

• “授权”指的是一个用户能否在你的应用中执行某个操作,在到达授权判断之前,身份的主题已经由 身份验证过程建立了。
这些概念是通用的,不是Spring Security特有的。在身份验证层面,Spring Security广泛支持各种身份验证模式,这些验证模型绝大多数都由第三方提供,或则正在开发的有关标准机构提供的。

  • 权限的概述
  • BRAC的模型:基于角色的访问控制模型。
  • 和SpringSecurity功能类似的框架。
  • shiro框架,apache组织开源框架。

入门:

1.导入相关maven依赖

<properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <spring.security.version>5.0.1.RELEASE</spring.security.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
  1. 配置tomcat 这一部分就不写了 看这个框架的没有小白
  2. 配置web.xml配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
<!--spring框架监听器,用来加载spring-security.xml配置文件事spring-security的核心配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-security.xml</param-value>
    </context-param>

    <!-- 委派过滤器  名称不能改变 这个过滤器是spring——security的和兴入口 -->
    <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>

</web-app>



委派过滤器的作用:

!委派过滤器作用

浏览器发送请求一定会经过委派过滤器 委派过滤器会请求Spring-Security.xml配置文件再判断当前tomcat服务器中你所请求的资源是否需要登录或者是否具有某种权限,如果具有权限就会放行,如果不具有权限就会跳转到登录页面 至于哪些资源能够访问 哪些资源需要什么样的权限不由他决定
Spring-Security相当于公司中的门卫 他能做的事情比你想象的少 只负责拦截 判断的活交给其他的做

spring-security.xml 配置文件

头信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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>
  • 配置访问资源的权限
    <!--配置访问资源的权限
    auto-config="true"  auto-config 表示自动配置
    值:
     true: 使用自动配置,框架提供登录和失败的页面
     false: 不使用自动配置,使用自己的登录页面


     use-expressions 是否使用表达式
     值:
     true: 使用Spring框架提供的表达式
     false:

    -->
    <security:http auto-config="true" use-expressions="true">
    <!--配置那些资源需要拦截 -->
</security:http>
  • 配置那些资源需要拦截
  <security:intercept-url pattern="/**"/>

配置一组模拟用户

<!--模拟一组用户
    authentication-manager 表示认证管理
    authentication-provider 表示认证的提供者
    user-service  表示提供用户的一组服务(配置一组用户)
    name=""         用户名
     password=""    密码(5.0以上的版本 需要考虑密码加密的问题) 如果是明文密码需要标识 表达式U{noop} 低版本不用考虑
      authorities=""   该用户拥有的角色 角色自己规定
    -->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"></security:user>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

入门完整配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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">
    <!--入门配置文件-->
    <!--

    配置访问资源的权限
    auto-config="true"  auto-config 表示自动配置
    值:
     true: 使用自动配置,框架提供登录和失败的页面
     false: 不使用自动配置,使用自己的登录页面


     use-expressions 是否使用表达式
     值:
     true: 使用Spring框架提供的表达式
     false:

    -->
    <security:http auto-config="true" use-expressions="true">
        <!--
            配置哪些资源需要拦截

            pattern="/**" 表示拦截所有资源,所有的资源都必须要登录才能访问
            pattern="/brand/**" 表示品牌模块下的所有资源都需要登录,才能访问
            access="" 表示是表达式
             access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"
             hasAnyRole  表示任意一个角色
             表示访问任何资源,都需要登录,并且具有ROLE_USER,或者ROLE_ADMIN角色才能访问
             角色名称写法:ROLE_XXXX   XXXX  表示任意名称  但是必须是ROLE_ 前缀 不可变
        -->
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

    </security:http>

    <!--模拟一组用户
    authentication-manager 表示认证管理
    authentication-provider 表示认证的提供者
    user-service  表示提供用户的一组服务(配置一组用户)
    name=""         用户名
     password=""    密码(5.0以上的版本 需要考虑密码加密的问题) 如果是明文密码需要标识 表达式U{noop} 低版本不用考虑
      authorities=""   该用户拥有的角色 角色自己规定
    -->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"></security:user>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

配置自己的登录页面
配置不拦截资源

 <!--配置某些资源为不拦截
    pattern  为不拦截的资源
    security 为权限  none 为不拦截
    -->
    <security:http pattern="/login.html" security="none"/>
    <!--配置静态资源不拦截-->
    <security:http pattern="/css/**" security="none"/>

配置登录页面

   <!--配置自己的登录页面页面
        login-page  表示配置自己的登录页面
        login-processing-url 表示配置 from表单提交的请求路径
         authentication-failure-forward-url  表示认证失败   认证失败后跳转的页面
         default-target-url  登录成功 跳转的登录页面
        -->
        <security:form-login
                login-page="/login.html"
                login-processing-url="/login"
                authentication-failure-forward-url="/login.html"
                default-target-url="/index.jsp"
        />

关闭跨域攻击

 <!--
        关闭跨域攻击
        true  表示失效
        想用自己的登录页面就必须关掉
        因为springSecurity不相信其他的登录页面 认为是跨域攻击 所以必须关闭
        -->
        <security:csrf disabled="true"/>

用户退出

<!--用户退出
         logout-url 表示你发送某个请求 就退出用户
         logout-success-url  表示用户退出后跳转的页面
         invalidate-session="true"  表示清除session
        -->
        <security:logout
                logout-url="/logout"
                logout-success-url="/login.html"
                invalidate-session="true"
        />

配置自己的登录页面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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">

    <!--配置某些资源为不拦截
    pattern  为不拦截的资源
    security 为权限  none 为不拦截
    -->
    <security:http pattern="/login.html" security="none"/>
    <!--配置静态资源不拦截-->
    <security:http pattern="/css/**" security="none"/>

    <security:http auto-config="true" use-expressions="true">
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
        <!--配置自己的登录页面页面
        login-page  表示配置自己的登录页面
        login-processing-url 表示配置 from表单提交的请求路径
         authentication-failure-forward-url  表示认证失败   认证失败后跳转的页面
         default-target-url  登录成功 跳转的登录页面
        -->
        <security:form-login
                login-page="/login.html"
                login-processing-url="/login"
                authentication-failure-forward-url="/login.html"
                default-target-url="/index.jsp"
        />
        <!--
        关闭跨域攻击
        true  表示失效
        想用自己的登录页面就必须关掉
        因为springSecurity不相信其他的登录页面 认为是跨域攻击 所以必须关闭
        -->
        <security:csrf disabled="true"/>
        <!--用户退出
         logout-url 表示你发送某个请求 就退出用户
         logout-success-url  表示用户退出后跳转的页面
         invalidate-session="true"  表示清除session
        -->
        <security:logout
                logout-url="/logout"
                logout-success-url="/login.html"
                invalidate-session="true"
        />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"></security:user>
                <security:user name="root" password="{noop}root" authorities="ROLE_USER"></security:user>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值