1. 程式人生 > >Spring Securty 應用(2)-- 自定義登入介面

Spring Securty 應用(2)-- 自定義登入介面

實現功能:

使用自定義的登入介面替換Spring Security預設的登入介面

配置參考 Spring Securty 應用(1)– 基於記憶體的認證

spring-security.xml 的配置

<http security="none" pattern="/resources/**"/> 對資原始檔不進行攔截
<http security="none" pattern="/login"/> 由於下面的配置/**是對所有的URL進行攔截,因此對logi頁面就不能進行攔截,否則會出現死迴圈
http security="none" 表示對指定的URL放行,不進行安全攔截
form-login login-page="/login" 即指定自定義的登入介面,如果不指定,則預設為Spring Security的預設登入頁面 j_spring_security_login
username-parameter="username" 指定登入頁面中使用者名稱的引數名稱,此處指定為username,如果不指定,則預設為j_username
password-parameter="password" 指定登入頁面中密碼的引數名稱,此處指定為password,如果不指定,則預設為j_password
<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" xmlns:mvc="http://www.springframework.org/schema/mvc"
             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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!-- 不進行安全攔截 --> <http security="none" pattern="/resources/**"/> <http security="none" pattern="/login"/> <http auto-config="true"> <!-- 指定登入頁面,如果不配置預設是 j_spring_security_login --> <form-login login-page="/login" username-parameter
="username" password-parameter="password"/>
<intercept-url pattern="/**" access="ROLE_USER"/> </http> <!-- 基於記憶體的認證 --> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="123456" authorities="ROLE_USER"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>

自定義Login.jsp 主要是參考Spring Security預設的登入介面的程式碼

此處為自定義登入頁面,需要特別注意的地方:
1.使用post方法提交
2.action指定為 /j_spring_security_check
3.使用者名稱引數名稱和密碼引數名稱要和前面 spring-security.xml 配置的一致,本文配置的為 username 和 password,這裡特別需要注意的是name的名稱,如果只指定了id沒有指定name,則也無法通過認證

認證通過之後會自動跳轉到ROOT目錄/下面,因此可以你把/指定一個jsp頁面,否則會出現 ”假的報錯頁面“


<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html>


<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="${base.contextPath}/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="${base.contextPath}/resources/bootstrap/login/signin.css" rel="stylesheet">
</head>

<body>

<div class="container">

    <form class="form-signin" action="/j_spring_security_check" method="post">
        <h2 class="form-signin-heading">Please login in</h2>
        <%--<label for="j_username" class="sr-only">User Name</label>--%>
        <%--<input type="text" id="j_username" name="j_username" class="form-control" placeholder="User Name" required autofocus>--%>

        <%--<label for="j_password" class="sr-only">Password</label>--%>
        <%--<input type="password" id="j_password" name="j_password" class="form-control" placeholder="Password" required>--%>


        <label for="username" class="sr-only">User Name</label>
        <input type="text" id="username" name="username" class="form-control" placeholder="User Name" required
               autofocus>

        <label for="password" class="sr-only">Password</label>
        <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>

        <div class="checkbox">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>

        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>

</div> <!-- /container -->


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="${base.contextPath}/resources/bootstrap/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>

Spring Security 預設登入頁面的程式碼如下

自定義的登入頁面實現主要參考 Spring Security預設登入頁面的程式碼實現的,下面為Spring Security 的登入頁面程式碼
從中可以可以看出關鍵屬性如下:
action=’/j_spring_security_check’
name=’j_username’
name=’j_password’

<html><head><title>Login Page</title></head><body onload='document.f.j_username.focus();'>
<h3>Login with Username and Password</h3><form name='f' action='/j_spring_security_check' method='POST'>
 <table>
    <tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
    <tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
    <tr><td colspan='2'><input name="submit" type="submit" value="Login"/></td></tr>
  </table>
</form></body></html>

Controller程式碼

此處配置login對應的頁面。登入成功之後Spring Security會預設跳轉到Root目錄(/)下面,此文配置/對應的為hello.jsp頁面

package com.cnblogs.duoduo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = "/login")
    public ModelAndView index() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Index - Spring Security Hello World");
        model.addObject("message", "This is index page!");
        model.setViewName("login");
        return model;

    }

    @RequestMapping(value = {"/", "/welcome"})
    public ModelAndView welcome() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Welcome - Spring Security Hello World");
        model.addObject("message", "This is welcome page!");
        model.setViewName("hello");
        return model;

    }

    @RequestMapping(value = "/admin")
    public ModelAndView admin() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Admin - Spring Security Hello World");
        model.addObject("message", "This is protected page!");
        model.setViewName("admin");

        return model;

    }

}