方法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>