項目下載:點擊下載
項目說明:基於SSH項目+Spring Security功能.
Spring Security配置文件講解
spring-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.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd ">
<!-- 指定登錄頁面不添加任何權限 -->
<http security="none" pattern="/login.jsp" />
<!-- 指定 訪問js文件不需要任何權限,這個不配置,jquery(js)文件引入不了哦 -->
<http security="none" pattern="/js/*.js" />
<!-- 指定 登錄處理action 不需要任何權限 -->
<http security="none" pattern="/login.action" />
<http auto-config="true">
<!-- login-page 設置自定義登錄頁面 -->
<!-- 認證成功處理:(1)用戶直接訪問登錄頁成功後,調轉 默認到項目根目錄,可以通過 default-target-url來設置 (2)用戶訪問其他頁面如a.jsp,跳轉到登錄頁,認證成功後到a.jsp;(3)也可通過設置always-use-default-target
屬性,只要認證成功就跳轉到該頁面 -->
<form-login login-page="/login.jsp" username-parameter="username"
password-parameter="password" />
<!-- 指定任何頁面都需要user權限(前面已經設置login.jsp不需要) -->
<intercept-url pattern="/**" access="ROLE_ADMIN" />
</http>
<authentication-manager alias="authenticationManager">
<!-- <authentication-provider user-service-ref="sysUserDetaisService" /> -->
<authentication-provider>
<user-service>
<user name="xiaotang" password="123" authorities="ROLE_USER" />
<user name="tangtang" password="456" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
http 標籤
security 是配置匹配網頁的權限,pattern爲匹配的格式,security爲訪問所需的權限:nono表示不需要任何權限,當然也可以設置爲 ROLE_USER和ROLE_ADMIN或自定義權限;
form-login 配置表單登錄項,如果不配置該項,則會使用spring Security默認的登錄頁
authentiaction-manager標籤:
authentiaction-manager提供用戶認證,它的認證需要依賴 authentication-provider,具體過程參照文章:http://wiki.jikexueyuan.com/project/spring-security/certification.html
登錄頁面請求
請求主要代碼如下:
<script>
$(function() {
$("input[type='button']").bind("click", function() {
$.ajax({
url : "login.action",
type : "post",
data : $("form").serialize(),
success : function(data) {
if (data == "success") {
//登錄成功,跳轉到主頁
window.location.href = "index.jsp";
} else {
alert("登錄失敗!");
}
}
});
});
});
</script>
</head>
<body>
<form>
<!-- 注意此處的用戶名和密碼的值一定要和 spring-security中配置的username和password相同 -->
用戶名:<input type="text" name="username"><BR> 密碼:<input
type="password" name="password"><BR> <input
type="button" value="提交">
</form>
</body>
Action處理
處理主要代碼如下:
public void login() {
try {
// 給用戶授權,沒有設置權限,默認給 ROLE_USER權限
/*
* UsernamePasswordAuthenticationToken authentication = new
* UsernamePasswordAuthenticationToken( username, password);
*/
// 給用戶授予 ROLE_USER,ROLE_ADMIN權限
List<GrantedAuthority> authorites = new ArrayList<GrantedAuthority>();
authorites.add(new SimpleGrantedAuthority("ROLE_USER"));
authorites.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
username, password, authorites);
SecurityContextHolder.getContext()
.setAuthentication(authentication);
this.getSession().setAttribute("SPRING_SECURITY_CONTEXT",
SecurityContextHolder.getContext());
this.getOut().print("success");
} catch (Exception e) {
this.getOut().print("error");
}
}
解釋:
大家可以通過上文的給鏈接瞭解了認證的過程, 其實就是如下兩行代碼:
SecurityContextHolder.getContext()
.setAuthentication(authentication);
this.getSession().setAttribute("SPRING_SECURITY_CONTEXT",
SecurityContextHolder.getContext());
session保存了名爲 SPRING_SECURITY_CONTEXT的 SecurityContext對象,並且這個對象有 相應的Authentication.此處Action中,我只是簡單的生成Authentication,並保存到
session中,後面的文章我會介紹通過與數據庫的比較來認證授權的。