SpringSecurity、Spring Social、SpringSession、TX-LCN、Spring Cloud Data Flow、JWT 架构(四)

继续SpringSecurity。今天我们聊SpringSecurity的标签库以及基于SpringSecurity的标签库和认证访问来动态的显示页面。SpringSecurity提供了一个较为完整的标签库,用这些标签我们可以控制访问的流程、权限、页面显示等。接下来我们用SpringSecurity的标签库来继续我们上面的案例。

第一步:在pom.xml中添加SpringSecurity的依赖:

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>4.0.1.RELEASE</version>
</dependency>

第二步:在需要控制显示页面中引入SpringSceurity的标签库,引入的方式jstl引入的方式一致:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ page session="true"%>

第三步: 在需要基于角色控制部分显示或全显示的这个页面,使用SpringSecurity标签: 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ page session="true"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>userWelCome</title>
</head>
    <body>
        ${description}
        <a href="/SecurityLogout/SpringSecurityPageController/toUserLogoutPageJsp">注销</a><br>
        <label>View all information| This part is visible to Everyone</label><br>
        <sec:authorize access="hasRole('ROLE_USER')">
            <label><a href="#">Edit this page</a> | This part is visible only to ADMIN</label><br>
        </sec:authorize>
        
        <sec:authorize access="hasRole('ROLE_ADMIN') and hasRole('ROLE_DBA') or hasRole('ROLE_baobao')">
            <label><a href="#">Start backup</a> | This part is visible only to one who is both ADMIN & DBA</label><br>
        </sec:authorize>
        
        
    </body>
</html>

第四步:在SpringSecurity.xml核心配置文件中添加:

全部的配置:

<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-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">
    
    <!--
    配置具体的规则:
    auto-config="true"    不用自己编写登录的页面,框架提供默认登录页面
    use-expressions="false"    是否使用SPEL表达式(没学习过)
    -->
    <beans:bean id="failureCDW" class="com.alibaba.failureCDW"></beans:bean>
    <http auto-config="true" use-expressions="true">
        <!--配置具体拦截的url,pattern是拦截的url,access是访问被拦截的url需要的权限-->
        <intercept-url pattern="/*" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/**/to**" access="hasRole('ROLE_USER') or hasRole('ROLE_DBA') or hasRole('ROLE_baobao')" />
        <intercept-url pattern="/**/gotoUserLoginPageJsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <!-- 定义跳转的具体页面
        form-login是spring security命名空间配置登录相关信息的标签,它包含如下属性:
        1. login-page 自定义登录页url,默认为/login
        2. login-processing-url 登录请求拦截的url,也就是form表单提交时指定的action
        3. default-target-url 默认登录成功后跳转的url
        4. always-use-default-target 是否总是使用默认的登录成功后跳转url
        5. authentication-failure-url 登录失败后跳转的url
        6. username-parameter 用户名的请求字段 默认为userName
        7. password-parameter 密码的请求字段 默认为password
        8. authentication-success-handler-ref 指向一个
           AuthenticationSuccessHandler用于处理认证成功的请求,不能和default-target-url
                      还有always-use-default-target同时使用
        9. authentication-success-forward-url 用于authentication-failure-handler-ref
        10. authentication-failure-handler-ref 指向一个AuthenticationFailureHandler用于处理失败的认证请求
        11. authentication-failure-forward-url 用于authentication-failure-handler-ref
        12. authentication-details-source-ref 指向一个AuthenticationDetailsSource,在认证过滤器中使用
        -->
        <form-login 
            login-page="/SpringSecurityPageController/gotoUserLoginPageJsp.do" 
            default-target-url="/SpringSecurityPageController/toUserWelComePageJsp.do" 
            authentication-failure-url="/SpringSecurityPageController/gotoUserLoginPageJsp.do?error=error" 
            always-use-default-target="true"
            username-parameter="username"
            password-parameter="password" />
        <!-- logout 属性详解
        logout-url LogoutFilter要读取的url,也就是指定spring security拦截的注销url
        logout-success-url 用户退出后要被重定向的url
        invalidate-session 默认为true,用户在退出后Http session失效
        success-handler-ref 对一个LogoutSuccessHandler的引用,用来自定义退出成功后的操作 
        这里需要注意的一点是,spring security 3.x默认的注销拦截url为/j_spring_security_logout,而4.x则默认使用/logout-->
         <logout logout-url="/SpringSecurityPageController/toUserLogoutPageJsp*"
                  logout-success-url="/SpringSecurityPageController/gotoUserLoginPageJsp" 
                  invalidate-session="true" />
         <!--关闭跨域请求,不关闭就会拦截所有的访问,显示权限不够-->
        <csrf/>
    </http>

    <authentication-manager>
      <authentication-provider>
        <user-service>
            <user name="chendawei" password="123456" authorities="ROLE_USER"/>
            <user name="cdw" password="123456" authorities="ROLE_ADMIN,ROLE_USER"/>
            <user name="zhaoxiuping" password="123456" authorities="ROLE_ADMIN,ROLE_DBA"/>
            <user name="baobao" password="123456" authorities="ROLE_baobao" />
        </user-service>
      </authentication-provider>
    </authentication-manager>

</beans:beans>
第五步:用不同的用户进行访问:

 1:先用chendawei这个用户访问,chendawei这个用户只有USER权限,所以可以看到下面的红色框中显示的话:

登录 

页面显示的内容: 

 用baobao这个用户访问,宝宝这个用户没有USER权限,所以看不到上图中显示的内容而可以看到下面的红色框中显示的话:

登录 

可以看到和我们预期的效果是一样的: 

 SpringSecurity框架基于角色的页面显示控制,聊到这。注意一下,这里在配置springsecutiy的时候, auto-config="true" use-expressions="true"都要置为true。不然会报No visible WebSecurityExpressionHandler instance could be found in the application。。。。的错。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值