Spring-Security (學習記錄二)--修改為自己的登錄頁面


1.修改spring-security.xml配置文件

<!--
    auto-config="true"      : 自動生成登錄頁面
    pattern="/admin/**"     : 為配置的路徑,**表示支持子目錄
    access="ROLE_ADMIN"     : 需要管理員權限,“ROLE_”前綴是一個提示Spring使用基於角色的檢查的標記。
    use-expressions="true"    : 表示access中支持hasRole這樣的函數
 -->
<security:http auto-config="false" use-expressions="true" access-denied-page="/login.jsp?error=access-denied-page">
    <!-- xml配置,配置的 pattern="/admin/**" 表示需要登錄才能訪問,而登錄的角色為ROLE_ADMIN
     -->
    <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
    <security:intercept-url pattern="/user/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />    
    
    <!-- 自己配置登錄頁面。默認登錄地址:j_spring_security_check -->
    <security:form-login default-target-url="/index.jsp" 
        username-parameter="username"
        password-parameter="password"
        authentication-failure-url="/login.jsp?error=authentication-failure-url"
        login-page="/login.jsp"
    />
    
</security:http>  

2.增加login.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄頁面</title>
</head>
<body>    
    ${param.error }
    <form action="j_spring_security_check" method="post">
        <table>
            <tr>
                <td>用戶名:</td>
                <td><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td>密碼:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="登錄" />
                    <input type="reset" value="重置" /></td>
            </tr>
        </table>
    </form>
</body>
</html>  

3.重啟項目即可看到效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM