在上一篇入门中,简单介绍了spring security3的用法,但现实中,登录页面都是用户自己定义的,而不是spring security3生产的,这个时候,我们可以自定义用户登录页面。通过分析spring security3生成的登录页面,我们可以看到,它是一个表单,表单的action,userName 和 password的name分别为 j_spring_security_check, j_username,j_password。
(1)针对这个情况,我们自定义登录页面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>login interface</title>
</head>
<body>
<h3>user login</h3>
${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
<form action="${pageContext.request.contextPath}/j_spring_security_check" method="post">
userName:<input type="text" name="j_username"/><br/>
password:<input type="password" name="j_password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
其中,${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}为spring security框架自带的信息,通过配置国际化文件,实现信息的提示,展开spring security core 可以看到国际化的文件,用户可以修改其内容满足自己项目的需要,也可自定义自己的国际化文件,将spring security core 中的key-value中的value进行修改,如果采用spring自带的国际化文件,请在配置文件中写入如下的配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!--****************************************************************************************-->
<!--**********************国际化支持信息配置*************************************************-->
<!--****************************************************************************************-->
<!-- 国际化支持 -->
<!-- Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="org.springframework.security.messages"/>
</bean>
</beans>
(2)修改spring-security配置文件满足自定义方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!--对登录页面不进行拦截,在页面后面加*表示,该页面后面可能会带一些参数-->
<security:http pattern="/login.jsp*" security="none"/>
<!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->
<security:http auto-config="true">
<!-- login-page: 指定登录页面-->
<security:form-login login-page="/login.jsp"/>
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
<!--配置认证管理器,只有用户名为user,密码为opal的用户,角色为ROLE_USER可访问指定的资源 -->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="opal" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
(3)运行该程序,系统自动跳到用户自定义的界面,输错代码,点击登录,错误信息提示如下:
(4)用户可以对密码进行加密,例如采用MD5的方式,假设用户密码为opal,其加密后为
22b5c9accc6e1ba628cedc63a72d57f8
此时,配置文件中的配置修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!--对登录页面不进行拦截,在页面后面加*表示,该页面后面可能会带一些参数-->
<security:http pattern="/login.jsp*" security="none"/>
<!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->
<security:http auto-config="true">
<!-- login-page: 指定登录页面-->
<security:form-login login-page="/login.jsp"/>
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
<!--配置认证管理器,只有用户名为user,密码为opal的用户,角色为ROLE_USER可访问指定的资源 -->
<security:authentication-manager>
<security:authentication-provider>
<security:password-encoder hash="md5"/>
<security:user-service>
<security:user name="user" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
- 大小: 39.6 KB
- 大小: 21.7 KB
分享到:
相关推荐
NULL 博文链接:https://gaojiewyh.iteye.com/blog/1501470
第4章 SpringSecurity安全详细讲解及应用 第5章 RabbitMQ高级消息的讲解及应用 第6章 ElasticSearch详细讲解及应用 第7章 Eureka分布式服务器的搭建 。。。。。。。。。 第15章 SpringCloud项目实战 。。。。。。。...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...
SpringBoot + SpringCloud + SpringSecurity学习过程中的二进制汇总,沉淀记录下学习历程 1.知识点图谱 所有博文集中发布在个人博客网站: 大致规划的内容包括以下章节,希望能用半年到一年(严重超期)的时间完成....
Spring Security是一个功能强大且高度可定制的身份验证和...这篇文章就是想通过一个小案例将Spring Security整合到SpringBoot中去。要实现的功能就是在认证服务器上登录,然后获取Token,再访问资源服务器中的资源。
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring ...
3.高级篇-单点登录,Groovy视频,性能优化,Maven,Mongodb,Redis,Spring Security,Spring 源码,JVM,微信,银行接口等 4.架构篇-Linux视频,Nginx视频,京东海量订单处理视频,大型电商分布式实践,架构实战等 ...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),...
Spring Boot迁移之路 - 哲学篇一 随着 Spring MVC 的使用,对复杂的部署流程、各种服务的集成、服务之间的影响、配置文件的编写都是耗时耗力的体力活。因此,Spring 开源组织诞生了 Spring Boot。本文主要讲述 ...
上载lombok大量简化了代码上一篇MyBatis Plus大量简化了SQL约会hutool工具包规范工具类♡minio分布式文件系统前一级完全脱离,前端代码可单独部署自定义Spring Security支持获取令牌账号密码:admin / admin版本
做开发时,查了大量资料,发现一比较全面的资料,分享一下! WebService CXF学习——入门篇 1.CXF由来 2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 ... 3.WS-Security WebService CXF学习——JAXB剖析
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),...
│ Struts+Hibernate+Spring轻量级J2EE企业应用实战.pdf │ Struts中文手册.pdf │ Struts配置文件详解.txt │ 上海税友.txt │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ ...
如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件机制的简单实现 我对J2EE和.NET的一点理解 难分难舍的DSO(一) InternalsVisibleToAttribute,友元程序集访问属性 Essential .NET 读书笔记 [第一部分] ...
│ Struts+Hibernate+Spring轻量级J2EE企业应用实战.pdf │ Struts中文手册.pdf │ Struts配置文件详解.txt │ 上海税友.txt │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ ...
│ Struts+Hibernate+Spring轻量级J2EE企业应用实战.pdf │ Struts中文手册.pdf │ Struts配置文件详解.txt │ 上海税友.txt │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ ...
│ Struts+Hibernate+Spring轻量级J2EE企业应用实战.pdf │ Struts中文手册.pdf │ Struts配置文件详解.txt │ 上海税友.txt │ 上海税友软件 面试题.doc │ 公司培训文档-混淆的基本概念.doc │ 基本算法.doc │ ...