• Spring Security 02


    权限管理

    配置不过滤的资源

    • 方法1
      <http pattern="/login.jsp" security="none"></http>

    • 方法2

    <sec:http auto-config="true">
         <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />配置表示允许匿名用户访问
    </sec:http>
    

    配置需要赋予权限才能访问的资源

    <http auto-config="true">
    		<!-- 表示访问app.jsp时,需要ROLE_ADMIN权限 -->
    		<intercept-url pattern="/adminpage.jsp" access="hasRole('ROLE_ADMIN')"></intercept-url>
    		<!--表示访问任何资源都需要ROLE_USER权限。 -->
    		<intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url>
    </http>
    

    自定义登陆登出页面

    applicationContext-security.xml配置

    <http auto-config="false" use-expressions="true">
    		<!-- 具有ROLE_ADMIN权限的用户才能访问全部路径 -->
    		<intercept-url pattern="/adminpage.jsp" access="hasRole('ROLE_ADMIN')"/>
    		<!-- 具有ROLE_USER权限的用户才能访问全部路径 -->
    		<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    		<form-login
    			login-page="/login.jsp"
    			login-processing-url="/j_spring_security_check"
    			authentication-failure-url="/login.jsp"
    			default-target-url="/index.jsp" />
    		<csrf disabled="true" />
    		<logout invalidate-session="true"
    			logout-success-url="/login.jsp"
    			logout-url="/j_spring_security_logout" />
    </http>
    
    • auto-config="true"时使用默认的配置,会配置十个默认过滤器:SecurityContextPersistenceFilter、LogoutFilter、
      UsernamePasswordAuthenticationFilter、BasicAuthenticationFilter、RequestCacheAwareFilter、SecurityContextHolderAwareRequestFilter、
      AnonymousAuthenticationFilter、SessionManagementFilter、ExceptionTranslationFilter、FilterSecurityInterceptor
    1. login-page="/login.jsp" 表示使用login.jsp代替默认登陆界面。
    2. login-processing-url="/j_spring_security_check" 使用spring-security 4.x版本必须添加该属性,表示登录表单提交路径。
    3. authentication-failure-url="/login.jsp" 表示授权失败之后跳转到login.jsp界面。
    4. default-target-url="/index.jsp" 表示授权成功之后默认跳转到index.jsp界面。
    5. logout-url="/j_spring_security_logout" 表示退出操作要提交到的url。
    6. logout-success-url="/login.jsp" 表示退出操作成功后跳转到的界面。

    页面

    • login.jsp
    <html>
        <body>
            <form action="j_spring_security_check" method="POST">
                <input type="text" name="username"  /> </br> 
                <input type="password" name="password" /> </br> 
                <input type="submit" value="submit" />
            </form>
        </body>
    </html>
    
    • index.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     
    <!DOCTYPE html>
    <html lang="en">
     
    <html>
    <body>
    <h2>this is a user page </h2>
    <a href="${pageContext.request.contextPath}/j_spring_security_logout">退出登陆</a>
    </body>
    </html>
    
  • 相关阅读:
    2018左其盛差评榜(截至10月31日)
    4星|《环球科学》2018年10月号:习惯悬崖上生活的游隼现在开心地在摩天大楼上捕食鸽子
    4星|《财经》2018年第25期:中国的五大主要城市群具备“大、快、活”三个独特优势
    2018左其盛好书榜(截至10月31日)
    4星|《门口的野蛮人1》:1988年惊动美国的一次杠杆收购事件
    在 ASP.NET Core 具体使用文档
    .Net Core 部署到 CentOS7 64 位系统中的步骤
    [Linux/Ubuntu] vi/vim 使用方法讲解
    CentOS7使用httpd apache 和firewalld打开关闭防火墙与端口
    Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序
  • 原文地址:https://www.cnblogs.com/nwu-edu/p/9424860.html
Copyright © 2020-2023  润新知