zoukankan      html  css  js  c++  java
  • SpringSecurity-eclipse

    1.1    SpringSecurity技术简介与使用

    1.1.1     简介

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。所有的操作都是以配置的形式操作。

    1.1.2     使用

    1.       导入依赖

        <!-- security身份验证 -->

            <dependency>

                <groupId>org.springframework.security</groupId>

                <artifactId>spring-security-web</artifactId>

            </dependency>

            <dependency>

                <groupId>org.springframework.security</groupId>

                <artifactId>spring-security-config</artifactId>

            </dependency>

    2.       配置web.xml文件

    <!-- springSecurity使用 -->

     <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring/spring-*.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>

    注意:其实springsecurity框架中所有的功能实现都是通过过滤器,那么我们过滤器的拦截形式有以下几种

    l  全目录 /

    l  后缀 *.do

    l  通配符 /开头

    经典错误:/*.do

    3.       配置spring-security.xml文件

    <?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">

     

       <!-- 以下页面不被拦截 -->

       <http pattern="/login*.html" security="none"/>

       <http pattern="/css/**" security="none"/>

       <http pattern="/img/**" security="none"/>

       <http pattern="/js/**" security="none"/>

       <http pattern="/plugins/**" security="none"/>

      

       <!-- 页面拦截规则

          use-expressions:默认是true,我们配置false,可以在 access直接写角色权限ROLE_USER

                      否则形式为 "hasRole('ROLE_USER')"

          intercept-url:表示拦截形式     /**   /*

          access:角色名称必须  "ROLE_*" 开头

        -->

       <http use-expressions="false">

          <intercept-url pattern="/**" access="ROLE_ADMIN" />

          <!-- 开启表单验证: security会自动生成一个验证的表单页面-->

          <form-login

             login-page="/login.html"

             default-target-url="/admin/index.html"

             always-use-default-target="true"

             authentication-failure-url="/login.html"

          />

          <!-- 关闭csrf 校验认证 -->

          <csrf disabled="true"/>

          <!-- 解决 in a frame because it set 'X-Frame-Options' to 'deny' 页面无显示问题

             约定   >  配置  >  编码

          -->

          <headers>

             <frame-options policy="SAMEORIGIN"/>

          </headers>

          <!-- 退出登录     -->  

          <logout/>

       </http>

     

       <!-- 认证管理器 -->

       <authentication-manager>

          <!-- 认证的提供者 -->

          <authentication-provider>

             <user-service>

                <user name="admin" password="123456" authorities="ROLE_ADMIN"/>

                <user name="sunwukong" password="dasheng" authorities="ROLE_ADMIN"/>

             </user-service>    

          </authentication-provider>

       </authentication-manager>

    </beans:beans>

     

    1.2        工作中遇到的这些问题

    1.       自定义登陆页面出现以下问题

    security="none"  设置此资源不被拦截.

    如果你没有设置登录页security="none"  ,将会出现以下错误

     

    分析问题:

    因为登录页会被反复重定向。

    如何解决:将login相关的页面放行

    <http pattern="/login*.html" security="none"/>

     

    2.       csrf disabled="true"  关闭csrf ,如果不加会出现错误

     

    分析:

    CSRFCross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用。

    使用Secuirty 3或更老的版本, 升级到4.0或之后的版本, Security将默认启用CSRF, 可防止跨站请求伪造登录, 或是用机器登录

      若不需要CSRF的场景(如对外的API接口), 可在Security配置中关闭

    解决<http中配置

           <csrf disabled="true"/>

    3.       登陆成功后会出现 in a frame because it set 'X-Frame-Options' to 'deny' 页面无显示问题

    分析:

    页面的返回头被设置 X-Frame-Options SAMEORIGIN ,只能被同源的iframe 引用。跨域名的iframe 没法显示了。

    解决:在<http中配置

       <headers>

          <frame-options policy="SAMEORIGIN"/>

       </headers>

    4.   启动报错org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 49 in XML document from class path resource [spring-security.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 49; columnNumber: 49; 元素 "dubbo:application" 的前缀 "dubbo" 未绑定。

    分析:

    Xml中没有引入标签,xml中配置中无法读取到dubbo相关的中配置的文件

    解决:在spring-security.xml中添加

        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

         http://code.alibabatech.com/schema/dubbo           http://code.alibabatech.com/schema/dubbo/dubbo.xsd

    smile
  • 相关阅读:
    Hibernate配置文件详解
    Struts工作原理、流程
    java三大框架原理
    JAVA三大框架的各自作用
    tomcat的种种
    jdk及tomcat的配置
    java-io-file
    JAVA-IO-文件过滤
    SPSS-单因素方差分析(ANOVA) 案例解析(转)
    SPSS-比较均值-独立样本T检验 案例解析(转)
  • 原文地址:https://www.cnblogs.com/qiuqiu001/p/8934569.html
Copyright © 2011-2022 走看看