spring security起步二:自定义登录页
2016-07-07 00:43
513 查看
在上一篇文章
spring security 起步:框架搭建 最后,我们可以看到spring security自动为我们生成了一个默认的登录页。首先呢 那个登录页太丑,其次呢登录时我们也想实现一些其他的功能:比如找回密码,Remember me等。那么 我们如何实现自定义登录页功能呢?
启动项目 可以看到我们自定义的登录页面
![](https://img-blog.csdn.net/20160707000632254?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
spring security 起步:框架搭建 最后,我们可以看到spring security自动为我们生成了一个默认的登录页。首先呢 那个登录页太丑,其次呢登录时我们也想实现一些其他的功能:比如找回密码,Remember me等。那么 我们如何实现自定义登录页功能呢?
添加登录页 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!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>Insert title here</title> <style> .error { padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px; color: #a94442; background-color: #f2dede; border-color: #ebccd1; } .msg { padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px; color: #31708f; background-color: #d9edf7; border-color: #bce8f1; } #login-box { width: 300px; padding: 20px; margin: 100px auto; background: #fff; -webkit-border-radius: 2px; -moz-border-radius: 2px; border: 1px solid #000; } </style> </head> <body onload='document.loginForm.username.focus();'> <div id="login-box"> <h2>请输入您的用户名与密码</h2> <form name='loginForm' action="" method='POST'> <table> <tr> <td>用户:</td> <td><input type='text' name='username' value=''></td> </tr> <tr> <td>密码:</td> <td><input type='password' name='password' /></td> </tr> <tr> <td colspan='2'><input name="submit" type="submit" value="登录" /></td> </tr> </table> </form> </div> </body> </html>
实现登录的Controller
@Controller public class LoginController { @RequestMapping(value = "/login", method = Requ 4000 estMethod.GET) public String loginPage() { return "login"; } }
配置spring security
在上面的配置中,通过指定url /login 跳转到login.jsp页面。那么接下来就是告诉spring security 我想使用自己定义的登录页,而且还要告诉spring security不能拦截我的登录页面。具体配置如下:<?xml version="1.0" encoding="UTF-8"?> <bean:beans xmlns="http://www.springframework.org/schema/security" xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <http pattern="/login" security="none"></http> <http auto-config="true" use-expressions="true"> <intercept-url pattern="/*" access="hasRole('ROLE_USER')" /> <form-login login-page="/login" /> </http> <authentication-manager alias="authenticationManager"> <authentication-provider> <user-service> <user authorities="ROLE_USER" name="guest" password="guest" /> </user-service> </authentication-provider> </authentication-manager> </bean:beans>
启动项目 可以看到我们自定义的登录页面
源码下载
https://github.com/SmallBadFish/spring_security_demo/archive/0.1.0-customlogin.zip相关文章推荐
- 《Head First Java》读书笔记(1)
- Java多维数组的应用
- springSecurity查看在线用户和下线用户
- SpringMVC解决多人开发路径可能重复的问题
- java并发机制之volatile详解
- Java 泛型 协变性、逆变性
- SpringMVC如何接受POST请求中的json参数
- SpringMVC 通过post接收form参数或者json参数
- 线程池
- hibernate缓存机制(三)
- 扩展SpringMVC以支持绑定JSON格式的请求参数
- spring security开篇
- Spring AOP 源码分析 part4 :拦截器的实现
- Spring注解@Component、@Repository、@Service、@Controlle
- 前序中序求后序的java算法
- Java 包装类和基本类 直接 默认值
- Quartz实现定时任务的配置方法(纯java作业调度框架)
- Java Socket编程
- 注释驱动的 Spring cache 缓存介绍
- 用maven快速搭建spring mvc的web项目(配置到jackson 和 任务调度)