spring security采用自定义登录页和退出功能
更新。。。
首先采用的是XML配置方式,请先查看 初识Spring security-添加security 在之前的示例中进行代码修改

<security:http auto-config="true">
<security:intercept-url pattern="/admin**" access="hasRole('ROLE_USER')"/>
<security:form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="user-name"
password-parameter="pwd"/>
<security:logout
logout-success-url="/login?logout"/>
<!-- XML 配置中默认csrf是关闭的,此处设置为打开
如果这里打开csrf,则在form表单中需要添加
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
-->
<security:csrf />
</security:http>
其中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>login</title>
<style>
.error {
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
color: #a94442;
background-color: #f2dede;
border: 1px solid #ebccd1;
}
.msg {
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
color: #31708f;
background-color: #d9edf7;
border: 1px solid #bce8f1;
}
#login-box {
width: 300px;
padding: 20px;
margin: 100px auto;
background: #fff;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border: 1px solid #000;
}
.hide {
display: none;
}
.show {
display: block;
}
</style>
</head>
<body>
<div id="login-box">
<h1>Spring Security 自定义登录页面</h1>
<div class="error" th:class="${error}? 'show error' : 'hide'" th:text="${error}"></div>
<div class="msg" th:class="${msg}? 'show msg' : 'hide'" th:text="${msg}"></div>
<form name='loginForm' action="/login" method='POST'>
<table>
<tr>
<td>用户名:</td>
<td><input type='text' name='user-name' /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type='password' name='pwd' /></td>
</tr>
<tr>
<td colspan='2'>
<input type="submit" value="提交" />
</td>
</tr>
</table>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
</form>
</div>
</body>
</html>
<form name='loginForm' th:action="@{/login}" method='POST'>
<table>
<tr>
<td>用户名:</td>
<td><input type='text' name='user-name' autofocus/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type='password' name='pwd' /></td>
</tr>
<tr>
<td colspan='2'>
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
使用这个表单也可以进行登录,提交时候自动添加_csrf属性
三、修改admin.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>admin</title>
</head>
<body>
<h1 th:text="|标题: ${title}|">Title : XXX</h1>
<h1 th:text="|信息: ${message}|">Message : XXX</h1>
<h2><a href="javascript:formSubmit()">Logout</a></h2>
<!-- csrf for logout-->
<form action="/logout" method="post" id="logoutForm">
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
</form>
<script>
function formSubmit() {
document.getElementById("logoutForm").submit();
}
</script>
</body>
</html>
<!-- 同样可以进行正常退出,自动加入了_csrf的属性值 -->
<form th:action="@{/logout}" method="post" id="logoutForm"></form>
如果使用这种表单也可以实现退出,提交时候自动添加_csrf属性
四、修改HelloController文件,添加代码:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "不正确的用户名和密码");
}
if (logout != null) {
model.addObject("msg", "你已经成功退出");
}
model.setViewName("login");
return model;
}
启动程序:访问http://localhost:8010/login 跳转到自定义的登录页面

package com.petter.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* 相当于spring-security.xml中的配置
* @author hongxf
* @since 2017-03-08 9:30
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 在内存中设置三个用户
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("hongxf").password("123456").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");
}
/**
* 配置权限要求
* 采用注解方式,默认开启csrf
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
.and()
.formLogin().loginPage("/login")
.defaultSuccessUrl("/welcome").failureUrl("/login?error")
.usernameParameter("user-name").passwordParameter("pwd")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
}
修改HelloController文件,添加
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "不正确的用户名和密码");
}
if (logout != null) {
model.addObject("msg", "你已经成功退出");
}
model.setViewName("login");
return model;
}
启动程序测试成功
spring security采用自定义登录页和退出功能的更多相关文章
- spring security之 默认登录页源码跟踪
spring security之 默认登录页源码跟踪 2021年的最后2个月,立个flag,要把Spring Security和Spring Security OAuth2的应用及主流程源码研究透 ...
- spring security使用自定义登录界面后,不能返回到之前的请求界面的问题
昨天因为集成spring security oauth2,所以对之前spring security的配置进行了一些修改,然后就导致登录后不能正确跳转回被拦截的页面,而是返回到localhost根目录. ...
- Spring Security笔记:自定义登录页
以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...
- spring security 之自定义表单登录源码跟踪
上一节我们跟踪了security的默认登录页的源码,可以参考这里:https://www.cnblogs.com/process-h/p/15522267.html 这节我们来看看如何自定义单表认 ...
- spring security采用基于简单加密 token 的方法实现的remember me功能
记住我功能,相信大家在一些网站已经用过,一些安全要求不高的都可以使用这个功能,方便快捷. spring security针对该功能有两种实现方式,一种是简单的使用加密来保证基于 cookie 的 to ...
- Spring-Security自定义登录页&inMemoryAuthentication验证
Spring Security是为基于Spring的应用程序提供声明式安全保护的安全性框架.框架下内容比较多,可以做到按照角色权限对请求路径进行限制.今天主要验证自定义登录页,在内存用户存储中进行请求 ...
- Spring Security 的注册登录流程
Spring Security 的注册登录流程 数据库字段设计 主要数据库字段要有: 用户的 ID 用户名称 联系电话 登录密码(非明文) UserDTO对象 需要一个数据传输对象来将所有注册信息发送 ...
- Spring Security在标准登录表单中添加一个额外的字段
概述 在本文中,我们将通过向标准登录表单添加额外字段来实现Spring Security的自定义身份验证方案. 我们将重点关注两种不同的方法,以展示框架的多功能性以及我们可以使用它的灵活方式. 我们的 ...
- CAS4.0.4 之自定义登录页实践
因最近公司要用到cas登陆而且要使用自定登陆页面,网络上搜索了一下cas自定义登陆,比较好的两篇文章CAS 之自定义登录页实践和CAS 之 跨域 Ajax 登录实践,感觉写的不错,但是发现改动的地方很 ...
随机推荐
- 组合使用QT的资源管理高级功能简化开发过程
使用 QT 进行团队开发的时候,常常碰到一个问题,就是如何共同管理资源?甚至一个人进行开发的时候如何简化资源的维护,避免无谓的消耗? 如果可以做到在开发的时候,大家把美工做的图片(往往是程序员先自己随 ...
- Oracle Schema Objects——Tables——Table Compression
Oracle Schema Objects Table Compression 表压缩 The database can use table compression to reduce the amo ...
- ORA-08002: sequence TESTTABLE1_ID_SEQ.CURRVAL is not yet defined in this session (未完全解决)
说明: 断开连接后 重新连接执行序列号当前值查找 会报错. 解决方法一:先查询序列号下一个值 SELECT testTable1_ID_SEQ.nextval from dual;
- MongoDB的Python客户端PyMongo(转)
原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...
- 利用kubeadm部署kubernetes 1.7 with flannel
一.Installation 1.安装环境为CentOS 7 2.安装Docker yum install -y docker systemctl enable docker systemctl st ...
- Django的model模型
一:字段选项 1,null =True 表示数据库的中可以存为null 默认值是False 2,blank=True 表示字段可以为空 默认值是False 3,chioces 由二项元组构成的一 ...
- android studio 中类似VS的代码折叠功能Region
1. 打开android studio 2. 选择要折叠的代码 3. 按Ctrl + Alt + T 选择 “region .. end region comments” Group selectio ...
- Java排序算法总结(转载)
排序算法 平均时间复杂度 冒泡排序 O(n2) 选择排序 O(n2) 插入排序 O(n2) 希尔排序 O(n1.5) 快速排序 O(N*logN) 归并排序 O(N*logN) 堆排序 O(N*log ...
- golang 实现并发计算文件数量
package main import ( "fmt" "io/ioutil" "os" ) func listDir(path strin ...
- (转)VLC播放RTP打包发送的.264文件
VLC播放RTP打包发送的.264文件 1,要有一个发送RTP包的264文件的服务器; 具体代码如下: rtp.h #include <WinSock2.h> #pragma commen ...