學習Spring-security(1)

        注:我寫這系列文章主要是記錄個人搭建一些心得,以及代碼。html

        Spring-Security是Spring提供的一整套完整的權限控制機制,比較簡單粗暴,直接經過配置文件的配置java

直接來判斷你是Pass or Out。而不用傳統的去發送請求給後臺代碼,後臺代碼查詢數據庫,判斷是否有訪問資源的權限,而後返回結果。還有一點就是Spring-Security能夠直接在內存層面進行過濾web

        Spring-Security環境的搭建。spring

        這裏我使用的是MyEclipse 9.0M1+JDK1.7+tomcat 6.0數據庫

  1. 先下載Maven以及Tomcat的對應的文件
    1. 下載Maven連接:http://maven.apache.org/download.cgi
    2. 下載Tomcat連接:http://tomcat.apache.org/
  2. 而後配置Myeclipse 使用下載的Maven
    1. window-》prefrences-》maven-》user setting 中配置User Settings (apache-maven-3.3.9\conf\settings.xml)
    2. window-》prefrences-》maven-》installations 中加入Maven主目錄
  3. 配置Tomcat/conf/tomcat_users.xml,增長一個超級用戶
    1. <role rolename="admin-gui"/>  
      <role rolename="admin-script"/>  
      <role rolename="manager-gui"/>  
      <role rolename="manager-script"/>  
      <role rolename="manager-jmx"/>  
      <role rolename="manager-status"/>  
      <user username="admin" password="admin" roles="manager-gui,manager-script,
      manager-jmx,manager-status,admin-script,admin-gui"/>
  4. 配置Maven/conf/settings.xml配置文件 加入admin/admin用戶
    1. <server>  
          <id>tomcat</id>  
          <username>admin</username>  
          <password>admin</password>  
      </server>

       

  5. 新建一個Maven Project 在pom.xml文件裏面加入以下依賴
      1. <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <springversion>3.1.1.RELEASE</springversion>
            <junitversion>3.8.1</junitversion>
          </properties>
          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
            </dependency>
             <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-asm</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context-support</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-expression</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jms</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-oxm</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-test</artifactId>
                    <version>${springversion}</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
         
                <dependency>
                    <groupId>javax.servlet</groupId>
        		    <artifactId>servlet-api</artifactId>
        		    <version>2.4</version>
        		    <scope>provided</scope>
                </dependency>
         
                <dependency>
                    <groupId>commons-collections</groupId>
                    <artifactId>commons-collections</artifactId>
                    <version>3.1</version>
                </dependency>
         
                <dependency>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                    <version>1.1</version>
                </dependency>
            	
            	<dependency>  
        	        <groupId>org.springframework.security</groupId>  
        	        <artifactId>spring-security-web</artifactId>  
        	        <version>3.2.0.RELEASE</version>  
        	    </dependency>  
        	    <dependency>  
        	        <groupId>org.springframework.security</groupId>  
        	        <artifactId>spring-security-config</artifactId>  
        	        <version>3.2.0.RELEASE</version>  
        	    </dependency>  
          </dependencies>

                此處引入了Spring的一系列核心jar包以及依賴包 注:注意其中的javax.servlet 這個依賴,由於這個依賴會和Tomcat裏面的lib的jar包衝突,致使報轉換異常express

        java.lang.ClassCastException: 
        org.springframework.web.filter.DelegatingFilterProxy cannot be cast to javax.servlet.Filter

        解決方案一:刪除Tomcat裏面的servlet.api.jar包           apache

    1.          解決方案二:修改scope屬性爲provided,Maven提供了scope的幾個屬性意思以下api

               * compile,缺省值,適用於全部階段,會隨着項目一塊兒發佈。 
               * provided,相似compile,指望JDK、容器或使用者會提供這個依賴。如servlet.jar。 
               * runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。 
               * test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發佈。 
               * system,相似provided,須要顯式提供包含依賴的jar,Maven不會在Repository中查找它。tomcat

  6. 配置Maven的JDK換進變量
    1. 選中一個JDk後再VM arguments裏面設置以下的變量: -          Dmaven.multiModuleProjectDirectory=$M2_HOME
  7. 最後右鍵項目 run as - maven build 便可

      最後Maven會自動的打包項目部署到tomcat裏面,項目已經啓動mvc

      參考文檔:http://blog.csdn.net/column/details/springsecurity.html

                        http://blog.csdn.net/maosijunzi/article/details/21160965

                        http://blog.csdn.net/lufeipeng/article/details/10469709

相關文章
相關標籤/搜索