SpringSecurity应用(一)
今天领导要了我们前端组从十月到年末的开发计划,发现年底的项目终于回归到了javascript做前端了,到时好好练练手。另外,发现以后可能会经常做权限管理,所以现在正好有点时间打算把SpringSecurity资料认真整理下,今天是入门级的,希望以后会越来越来越深入。
java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downloads.html。不过我的项目里的jar包比较旧点了,是从以前项目抽取出来的,我的工程结构图如下:
第一个实例:
第一个例子是最基本,最简单的,我第一次接触springsecurity时候觉得这个技术真惊艳,不过现在感觉也就那么回事了。
我首先编写的是web.xml:
<?xml version= "1.0" encoding= "UTF-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://java.sun.com/xml/ns/javaee"
xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id= "WebApp_ID" version= "2.5" >
<display-name>SpringSecurityPrj</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext*.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter- class >org.springframework.web.filter.DelegatingFilterProxy</filter- class >
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
接下来编写的是applicationContext-security.xml文件:
<?xml version= "1.0" encoding= "UTF-8" ?>
<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"
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.0.xsd">
<!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
<http auto-config= "true" >
<intercept-url pattern= "/**" access= "ROLE_USER" />
</http>
<!-- 认证管理器。用户名密码都集成在配置文件中 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name= "sharp" password= "sharp" authorities= "ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
|
另外我新建了一个index.jsp文件,作用是登录成功后返回到index.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>
<span color= "red" >登录成功!</span>
</body>
</html>
|
当我们在浏览器地址栏里输入下面的url:
http: //localhost:8080/SpringSecurityPrj/
|
我们就可以再浏览器里看到用户登录界面:
呵呵,内置的登录页面,挺好玩的。没有使用过springsecurity可能还没发现我在那里配置用户名和密码吧,看下面一段代码,这里就是用户名和密码:
<user name= "sharp" password= "sharp" authorities= "ROLE_USER" />
|
测试一:
我们录入用户名:admin;密码:admin,然后点击提交查询,最终页面如下:
登录失败了哦!
测试二:我们录入用户名:sharp;密码:sharp;如下图:
点击提交查询后,页面如下:
页面跳转到index.jsp页面,登录成功了。
哈哈,用这个做登录是不是很easy啊!
(博主温馨提示:我开始做测试是使用myeclipse,所有操作都没问题,然后改用eclipse-java EE,每次启动tomcat,eclipse都报了Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor错误,启动了45秒后tomcat自动停止,我在百度查原因,找到了一个解决方法,解决方法如下:
Server Tomcat v6. 0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
修改 workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml文件。
<servers>
<server hostname= "localhost" id= "JBoss v5.0 at localhost" name="JBoss v5. 0 at
localhost " runtime-id=" JBoss v5. 0 " server-type=" org.eclipse.jst.server.generic.jboss5"
server-type-id= "org.eclipse.jst.server.generic.jboss5" start-timeout= "1000" stop-
timeout= "15" timestamp= "0" >
<map jndiPort= "1099" key= "generic_server_instance_properties" port= "8090"
serverAddress= "127.0.0.1" serverConfig= "default" />
</server>
</servers>
把 start-timeout= "45" 改为 start-timeout= "1000" 或者更长
重启eclipse就可以了。
这个原因就是:启动tomcat需要的时间比 45 秒大了,Eclipse会判断tomcat在默认的时间是否启动了,如果在默认 45 秒没有启动就会报错了。
|
)
在第一个实例基础上我做了第二个实例。
第二个实例:
第一个例子里的登录页面是springsecurity的默认页面,这种死板的页面满足不了千变万化的用户需求,因此这个实例里我将自定义登录界面,这里我们还要加入几个jar包,最新的lib包下的目录如下所示:
新建一个login.jsp页面代码如下:
<% @taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>
<%@ 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 onLoad= "document.f.j_username.focus();" >
<c: if test= "${not empty param.login_error}" >
<font color= "red" >
登录失败,请重试.<br/><br/>
原因:<c:out value= "${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</font>
</c: if >
<form name= "f" action= "<c:url value='j_spring_security_check'/>" method= "POST" >
<table>
<tr>
<td>用户名:</td>
<td>
<input type= 'text' name= 'j_username' value= '<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>' />
</td>
</tr>
<tr>
<td>密 码:</td>
<td><input type= 'password' name= 'j_password' ></td>
</tr>
<tr>
<td>
<input type= "checkbox" name= "_spring_security_remember_me" ></td><td>两周内自动登录
</td>
</tr>
<tr>
<td colspan= '2' align= "center" >
<input name= "submit" type= "submit" >
<input name= "reset" type= "reset" >
</td>
</tr>
</table>
</form>
</body>
</html>
|
修改applicationContext-security.xml配置文件:
<?xml version= "1.0" encoding= "UTF-8" ?>
<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"
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.0.xsd">
<!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
<http auto-config= "true" >
<intercept-url pattern= "/login.jsp*" access= "IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern= "/**" access= "ROLE_USER" />
<form-login login-page= "/login.jsp" authentication-failure-url= "/login.jsp?login_error=1" />
</http>
<!-- 认证管理器。用户名密码都集成在配置文件中 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name= "sharp" password= "sharp" authorities= "ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
<!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: -->
<beans:bean id= "messageSource"
class = "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
<beans:property name= "basename" value= "classpath:org/springframework/security/messages_zh_CN" />
</beans:bean>
</beans:beans>
|
我们在浏览器地址栏里输入下面的url,点击回车,界面如下:
测试一:
我们录入用户名:admin;密码:admin,然后点击提交查询,最终页面如下:
登录失败!
测试二:我们录入用户名:sharp;密码:sharp;如下图:
点击提交查询,结果如下:
第三个实例:
只要是接触过权限管理的程序员都知道,一般的权限管理都有角色的概念,但是传统的角色都是在数据库建模,然后用编程的方式来实现的。在springsecurity里面就有角色的概念,用起来也很方便,上面的例子里我们使用了一个角色ROLE_USER,现在我们添加一个角色ROLE_ADMIN,我们修改applicationContext-security.xml配置文件:
<?xml version= "1.0" encoding= "UTF-8" ?>
<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"
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.0.xsd">
<!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->
<http auto-config= "true" >
<intercept-url pattern= "/login.jsp*" access= "IS_AUTHENTICATED_ANONYMOUSLY" />
<!-- 增加 ROLE_ADMIN角色-->
<intercept-url pattern= "/admin.jsp" access= "ROLE_ADMIN" />
<intercept-url pattern= "/**" access= "ROLE_USER" />
<form-login login-page= "/login.jsp" authentication-failure-url= "/login.jsp?login_error=1" />
</http>
<!-- 认证管理器。用户名密码都集成在配置文件中 -->
<authentication-manager>
<authentication-provider>
<user-service>
<!-- 添加ROLE_ADMIN角色 -->
<user name= "admin" password= "admin" authorities= "ROLE_USER,ROLE_ADMIN" />
<user name= "sharp" password= "sharp" authorities= "ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
<!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: -->
<beans:bean id= "messageSource"
class = "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
<beans:property name= "basename" value= "classpath:org/springframework/security/messages_zh_CN" />
</beans:bean>
</beans:beans>
|
另外我新建一个admin.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>Admin 管理界面</title>
</head>
<body>
<p style= "color:red" >admin.jsp页面</p>
</body>
</html>
|
修改下index.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>
<span color= "red" >登录成功!</span>
<br/>
<a href= "admin.jsp" >admin.jsp</a>
</body>
</html>
|
测试一:
我们输入的用户名:sharp;密码:sharp,登录成功了,我们进入到了页面index.jsp:
点击admin.jsp链接,结果如下:
sharp用户没有ROLE_ADMIN角色的权限,所以sharp访问不了admin.jsp页面。
测试二:
我们输入的用户名:admin;密码:admin,登录成功了,我们进入到了页面index.jsp(如上图),
然后
点击admin.jsp链接,结果如下:
用户admin是可以访问admin.jsp页面。
好了,今天学习结束了!
总结下:今天都是具体操作,而且这些操作在网上多的不得了,不过我想学springsecurity都得从这一步开始,现在我对springsecurity理解还不深,整篇文章都是如何去编码,而没有一些知识的讲解,我会尽全力一步步深入,做软件最好还是知其所以然,明天看能不能研究原理,下一篇由springsecurity和数据库的结合开始。
SpringSecurity应用(一)相关推荐
- SpringSecurity安全验证中文乱码问题
使用SpringSecurity做安全验证时发现form表单中提交中文名会出现乱码问题. 原因是因为我在web.xml配置文件中将springSecurityFilterChain拦截器放在了 cha ...
- Spring源码分析【8】-分布式环境SpringSecurity保持用户会话
1.SpringSecurity的权限控制流程是这样的: 用户登录,基础信息UserInfo存在SpringSecurity的ThreadLocal里. 下面是contextHolder对象: fin ...
- SpringSecurity学习:1(第一个SpringSecurity项目)
此博客是记录自己学习过程的记录 第一个SpringSecurity项目 导入依赖 详细的步骤我就不多说了,使用IDEA创建过SpringBoot项目的人一般都能看懂. 这一步我们可以在使用IDEA创建 ...
- Java项目:在线淘房系统(租房、购房)(java+SpringBoot+Redis+MySQL+Vue+SpringSecurity+JWT+ElasticSearch+WebSocket)
源码获取:博客首页 "资源" 里下载! 该系统有三个角色,分别是:普通用户.房屋中介.管理员.普通用户的功能:浏览房屋信息.预约看房.和中介聊天.申请成为中介等等.房屋中介的功能: ...
- SpringSecurity使用 配置文件 和wen.xml 文件配置
目录 1.web.xml 文件配置 2.spring-security 普通 为使用自己创建的认证类 1.web.xml 文件配置 !-- 配置SpringSecurity的拦截器 -->&l ...
- SpringBoot+SpringSecurity前后端分离+Jwt的权限认证(改造记录)
欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/zzzgd_666/article/details/96444829 前言 一般来说,我们用Spr ...
- echarts前后端交互数据_SpringBoot2.0实战(26)整合SpringSecurity前后端分离JSON交互...
在前端的文章中,我们实现了使用 SpringSecurity 实现登录鉴权,并使用数据库存储用户信息,实现登录鉴权 SpringBoot2.0实战(24)整合SpringSecurity之最简登录方法 ...
- springboot整个cas_SpringBoot集成SpringSecurity+CAS
1 简介 本文主要讲述如何通过SpringSecurity+CAS在springboot项目中实现单点登录和单点注销的功能. 2 项目依赖 主要依赖如下 org.springframework.boo ...
- Spring-security配置
为什么80%的码农都做不了架构师?>>> Spring-security配置 Spring安全组件的配置 一.intercept-url配置 1. 访问权限 <!-- 获 ...
- SpringSecurity权限表达式
* 当我们想要使用多个权限表达式的时候,是不能直接级联调用的,也就是说,我们只能手写了. 1 @Override 2 protected void configure(HttpSecurity htt ...
最新文章
- 用python画时序图源代码_使用python实现画AR模型时序图
- SAP RETAIL 商品主数据里影响自动补货结果的几个参数 I
- 学会动态丨中国人工智能学会重磅发布《2018人工智能产业创新评估白皮书》
- AJAX俺也不会,是真的,不过,以后就会了
- js GB2312和unicode互转
- ubuntu下安装极点五笔
- c语言ecit,Arthritis Rheumatol:新型JAK3/TEC抑制剂PF-06651600(ritlecitinib)对中重度类风湿性关节炎的疗效和安全性...
- Java中给循环体起别名
- 22(2)序列化以及反序列化
- 方差分析 球形检验_两因素重复测量设计做方差分析时,球形检验没有结果怎么回事?...
- Shell中 2/dev/null
- Visual Studio Code 1.51 发布
- 简单的动态JavaScript Ajax函数
- 客栈V4.21:多一个作品便多一份工作机会
- python的most_common()函数
- 程序猿麒麟臂打造之路(健身一)
- 【TLD】改进后的TLD视频目标跟踪方法的MATLAB仿真
- python selenium 隐藏浏览器_来了!最完美方案!Selenium模拟浏览器如何正确隐藏特征...
- SSCOM串口软件模拟TCP通信接发信息
- 苹果笔记本开机问号白色问号?