您的位置:首页 > 编程语言 > Java开发

保护Spring应用之——spring Security(二)

2014-12-13 21:26 423 查看

</pre><span style="font-size:18px;"><span style="white-space:pre">	</span>书接前文,Security为我们提供类一个免费的登陆页面,但是项目中的页面都是规定好的,所以必须替换掉,并且在登陆后进入首页的时候,一般也需要现实当前登陆的用户的一些信息,那么这些如何做呢,今天就和大家分享一下!</span></h3><div><span style="font-size:18px;"><span style="white-space:pre">(一)</span>首先,让我们看一下Security为免费登陆页面的源码:</span></div><div><span style="font-size:18px;"></span><pre name="code" class="html"><html>
<span style="white-space:pre">	</span><head>
<span style="white-space:pre">		</span><title>Login Page</title>
<span style="white-space:pre">	</span></head>
<span style="white-space:pre">	</span><body onload='document.f.j_username.focus();'>
<span style="white-space:pre">		</span><h3>Login with Username and Password</h3>
<span style="white-space:pre">		</span><form name='f' action='/<span style="color:#ff6666;"><strong>springSecurity</strong></span>/j_spring_security_check' method='POST'>
<span style="white-space:pre">			</span><table>
<span style="white-space:pre">				</span><tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
<span style="white-space:pre">				</span><tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
<span style="white-space:pre">				</span><tr><td colspan='2'><input name="submit" type="submit" value="Login"/></td></tr>
<span style="white-space:pre">			</span></table>
<span style="white-space:pre">		</span></form>
<span style="white-space:pre">	</span></body>
</html>


分析源码,我们看出,他from请求的地址是:/项目名称/j_spring_security_check,用户名输入框的name=“j_username”,密码输入框的name=“j_password”,有了这些,接下来就可以改写登陆页面啦~!

(二)修改applicationContext-security.xml配置文件中额配置信息:
<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.xsd"> 
<!-- 存放不带任何前缀的安全性配置元素 -->
<!-- spring-security赠送一个登陆页面 -->
<!-- <http>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login />
<logout />
</http>  -->

<!-- 自定义登陆界面 -->
<http >
<!-- IS_AUTHENTICATED_ANONYMOUSLY 角色只能访问login.jsp页面 -->
<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"/>
<!-- 退出登陆 -->
<logout logout-url="/springSecurity/j_spring_security_logout"/>
</http>

<authentication-manager>
<authentication-provider>
<user-service>
<!-- 配置了ROLE_USER, ROLE_ADMIN 角色的用户名和密码 -->
<user name="jimi" password="1234" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="5678" authorities="ROLE_USER" />
<user name="hell" password="0000" authorities="IS_AUTHENTICATED_ANONYMOUSLY" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>


在这里我们又增加了一个角色,这个角色只能访问指定页面!好了,我们只要在项目的WebContent文件夹下新建login.jsp就好了(注意:form的action路径,用户名和密码的name值要与之前的一致)

(三)如果要的在登陆成功的页面得到登陆账户的信息,需要在jsp中引入
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
同时为项目加入spring-security-taglibs-3.2.5.RELEASE.jar包
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!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>
</head>
<body>
<div>
<h1>welcome</h1>
<!-- 获取登陆名 -->
<sec:authentication property="principal.username"/>
</div>
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: