SpringSecurity3網站安全授權

狗尾巴呢發表於2011-11-01
繼續我們的Spring Roo之旅,今天看看站點安全的使用,roo中整合了Spring security,命令列下輸入

security setup,即自動建立相關的配置和依賴。然後可以使用spring security的各種基礎設施了。

 

不過對於一般的網站設定也很簡單,具體如下: 

1、 Spring Security XML configuration file 配置檔案

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans:beans xmlns=”http://www.springframework.org/schema/security”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:beans=”http://www.springframework.org/schema/beans”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.0.xsd
“>
<http auto-config=”true”>
<intercept-url pattern=”/*” access=”ROLE_USER”/>
</http>
<authentication-manager alias=”authenticationManager”>
<authentication-provider>
<user-service>
<user authorities=”ROLE_USER” name=”guest” password=”guest”/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
2、 Adding the Spring DelegatingFilterProxy to your web.xml file 
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filterclass>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、 Adding the Spring Security XML configuration file reference to web.xml
兩個形式:
A: web.xml中有Servlet
<servlet>
<servlet-name>dogstore</servlet-name>
<servletclass>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
As the name of the servlet (<servlet-name>) is dogstore, Spring`s Convention over Configuration (CoC) rules will search for an XML configuration file called dogstore-servlet.xml in WEB-INF.
B:
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
/WEB-INF/dogstore-security.xml
/WEB-INF/dogstore-base.xml
</param-value>
   </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

詳細的可以參考如下的這本書,特別是附書的程式碼很有參考價值 

Spring Security 3 Secure your web applications against malicious intruders with this easy to follow practical guide 

http://www.packtpub.com/spring-security-3/book 

如果需要更方便的許可權處理,可以參考國人開源的一箇中介軟體 ralasfe

http://www.ralasafe.cn/


相關文章