天天看點

【Spring Security】之一:Eclipse搭建Spring Security項目

寫在前面的話:本人小白,僅供參考。

一、原材料

1、Eclipse軟體,直接從官網下載下傳;

2、SpringFramework的全套jar包:spring-framework-4.1.7.RELEASE-dist.zip;

3、Spring Security的全套jar包:spring-security-4.0.1.RELEASE-dist.zip;

4、SpringFramework依賴的jar包:commons-logging-4.0.6.jar,log4j-1.2.16.jar;

二、工序

1、Eclipse建立Dynamic Web類型(動态網頁)的工程,注意自動生成web.xml檔案(當然建立工程的時候不生成也可以後續手動添加)。

工程目錄如下:

【Spring Security】之一:Eclipse搭建Spring Security項目
【Spring Security】之一:Eclipse搭建Spring Security項目

将(一)中2、3、4項包含的jar包全部拷貝到WebContent/WEB-INF/lib下;

2、配置web.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
           
<span>	</span>xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
           
<span>	</span>id="WebApp_ID" version="3.1">
    <display-name>security</display-name>
  
    <!--
      - Location of the XML file that defines the root application context
      - Applied by ContextLoaderListener.
      -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
            /WEB-INF/config-security.xml
        </param-value>
    </context-param>
    
    <!-- 配置Security -->
    <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>
  
    <!-- 配置Spring的Web應用 -->
    <!--
      - Loads the root application context of this web app at startup.
      - The application context is then available via
      - WebApplicationContextUtils.getWebApplicationContext(servletContext).
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
</web-app>
           

3、配置config-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-4.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-4.0.xsd">
         
    <http>
<span>	</span><intercept-url pattern="/**" access="hasRole('USER')" />
<span>	</span><form-login />
<span>	</span><logout />
    </http>

    <!-- koala: koala   emu:emu   wombat:wombat -->
    <authentication-manager>
        <authentication-provider>
            <password-encoder hash="md5"/>
            <user-service>
                <user name="koala" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
                <user name="emu" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
                <user name="wombat" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
                        
</beans:beans>                       
           

4、配置applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- Root Context: defines shared resources visible to all other web components -->

     
</beans>
           

5、編輯index.xml

<!doctype html>
<html>
<head>
    <title>Spring Security</title>
</head>
<body>
    <h1>Hello Sprig Security!</h1>
    <hr>
</body>
</html>
           

三、驗收

至此,我們并沒有寫任何的java代碼,隻是配置了Spring Security到我們的web工程中來并配置了簡單的登入驗證。我們可以看到如下效果:

【Spring Security】之一:Eclipse搭建Spring Security項目
【Spring Security】之一:Eclipse搭建Spring Security項目
【Spring Security】之一:Eclipse搭建Spring Security項目
【Spring Security】之一:Eclipse搭建Spring Security項目

                                     圖1 在tomcat伺服器運作index.xml檔案                                                                  圖2 使用者名/密碼不比對時

【Spring Security】之一:Eclipse搭建Spring Security項目
【Spring Security】之一:Eclipse搭建Spring Security項目

                                  圖2 使用者名/密碼比對時

繼續閱讀