方法1
<http pattern="/login.jsp" security="none"></http>
html
<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>
<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>
<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>
<%@ 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>