1. 程式人生 > >(一)如何使用Spring-security來實現登入驗證功能(XML配置方式)?

(一)如何使用Spring-security來實現登入驗證功能(XML配置方式)?

先從使用xml的方式來實現使用者的許可權登入

(1)需要在maven工程中加上關於spring-secutity的jar包的依賴

//spring-securityd 有關的依賴
    <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency
>
<groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency>

(2)建立一個spirng-security的配置檔案

<?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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"
>
<!-- 設定頁面不登陸也可以訪問進行訪問所有的靜態資源。這裡可以自行配置 --> <http pattern="/*.html" security="none"></http> <http pattern="/css/**" security="none"></http> <http pattern="/img/**" security="none"></http> <http pattern="/js/**" security="none"></http> <http pattern="/plugins/**" security="none"></http> <!-- 頁面的攔截規則 use-expressions:是否啟動SPEL表示式 預設是true --> <http use-expressions="false" > <!-- 當前使用者必須有ROLE_USER的角色 才可以訪問根目錄及所屬子目錄的資源 --> <intercept-url pattern="/**" access="ROLE_ADMIN"/> <!-- 開啟表單登陸功能 --> <form-login login-page="/login.html" default-target-url="/admin/index.html" authentication-failure-url="/login.html" always-use-default-target="true"/> <csrf disabled="true"/> <headers> <frame-options policy="SAMEORIGIN"/> </headers> <logout/> </http> <!-- 認證管理器 --> <authentication-manager> <authentication-provider> <user-service> //這個是你配置的可以登入的使用者,authorities的值必須是ROLE_XXX的命名方式 <user name="admin" password="123456" authorities="ROLE_ADMIN"/> <user name="sunwukong" password="dasheng" authorities="ROLE_ADMIN"/> <user name="wangwei" password="123456" authorities="ROLE_ADMIN"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>

(3)還需要在web.xml檔案中進行配置一個springSecurityFilterChain的過濾鏈

    //這裡是把(2)步驟中新增的檔案進行引用
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-security.xml</param-value>
     </context-param>
     <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
     </listener>
    <!-- 這個是配置的spring-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-security實現登入驗證功能XML配置方式

先從使用xml的方式來實現使用者的許可權登入 (1)需要在maven工程中加上關於spring-secutity的jar包的依賴 //spring-securityd 有關的依賴 <

如何使用spring-security實現使用者的登入許可權功能配合使用資料庫的方式

如何使用spring-security來實現使用者的登入功能之配合使用資料庫的方式 這個圖大家先熟悉一下簡單的過一遍,等把步驟都寫完之後,後面會總結 (一)使用spring-security之前需要做的準備(基於springMVC和dubbo的專案)

Android開發中使用FileDownloader實現檔案下載功能總結

今天研究了一下Android開發中檔案下載功能,記錄一下。這篇部落格主要介紹第三方下載外掛:FileDownloader的單任務的使用方法,至於多工的後面會做補充記錄,再寫一篇博文。效果圖如下:(虛擬機器連不上網)1、首先是引用方法:implementation 'com.l

springboot+spring security+mybaits實現登入許可權管理

序:本文采用springboot+spring security+mybatis來解決登入許可權管理的問題。由於是新人,所以在操作方面講得比較詳細。話不多說,直接來開始我們的專案。1 建立springboot+spring security+mybatis 專案這裡我們採用了

實現登入註冊功能連線資料庫的JSP小專案

一、實現登入功能首先用sublime製作有表單檢查功能的login.html,程式碼具體如下:為了簡潔,我在Eclipse裡將CSS的引用方式改為鏈入式了,而接下來是在sublime剛做好後的程式碼截圖:然後將<form>改為<formaction="chk

環信sdk實現登入註冊功能第三方通訊

最近寫聊天軟體,需要第三方通訊實現聊天功能,下面再分享一下環信sdk整合(個人心得:我是個新手,其實整合環信sdk我之前用了接近一個星期參考官方文件,也百度瞭解了很多,最後整合失敗了,,實在是心疼就放棄了環信,接著去學極光sdk的整合,極光弄成功之後再回來繼續研究環信的,給我

Spring Security實現圖形驗證功能實現"記住我"功能

說在前面 博主最近會有很多專案跟大家一起分享,做完後會上傳github上的,希望讀友們能給博主提提意見哈哈 這個專案是第三方登入和安全方面的,關於後臺與a

javaWeb實現郵箱驗證功能

上篇寫的實現基本上是一個工具類,並沒封裝成下圖這樣方式(梨視訊註冊介面),現在開始完善上篇的java郵箱啟用驗證功。 java郵箱驗證地址:https://blog.csdn.net/BlackPlus28/article/details/85044068 -----------------

Spring Security 自定義登入驗證與自定義回撥地址

1 配置檔案 security-ns.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/b

javaWeb(十三Demo:Filter實現自動登陸功能Myeclipse+JSP+Servlet+EL/JSTL+Filter

Filter實現自動登陸功能 原始碼:source code 思路圖: 就是在伺服器和顯示介面之間新增一個過濾器(這裡過濾範圍其實是全域性),獲取cookie裡面的使用者名稱和密碼,然後直接在Filter裡面登陸(就不要去登陸頁面了) 登陸頁面主要是看你的sess

spring mvc+spring+mybatis+ajax實現登入驗證

<h1>Spring Mvc+Spring+Mybatis+Ajax 實現非同步登入的例子,和大家分享一下。</h1><div>login.js程式碼:</div><pre name="code" class="javas

Spring整合Hibernate管理事務xml配置方式

之前是剛開始使用Spring的事務管理,採用比較簡單方便的註解方式進行事務處理,而註解方式進行事務處理需要在每一個要實現事務的類上新增@Transactional註解,在比較小的專案中可以這樣來用。但是在大型專案中,註解方式管理事務不夠清晰,無法判斷哪裡開啟了事務,必須一一

spring aop的使用註解方式以及基於xml配置方式

註解方式 ******************* beans.xml ******************* <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springf

詳解Spring Security的HttpBasic登入驗證模式

一、HttpBasic模式的應用場景 HttpBasic登入驗證模式是Spring Security實現登入驗證最簡單的一種方式,也可以說是最簡陋的一種方式。它的目的並不是保障登入驗證的絕對安全,而是提供一種“防君子不防小人”的登入驗證。 就好像是我小時候寫日記,都買一個帶小鎖頭的日記本,實際上這個小鎖頭有什

Django實現登入驗證功能

Django實現登入驗證功能Django實現登入驗證功能:Django對使用者登入功能已經進行了封裝,我們只需要簡單地修改就可以了。 檢視:views.py # Create your views

Spring Security OAuth2 第三方登入之流程說明

SpringBoot第三方登入流程 OAuth2AuthenticationService 該類執行獲取code,token,建立connection,由SocailAuthenticationFilter呼叫 public class OAuth2Authenticati

Spring Security技術棧開發企業級認證與授權Spring Security的基本執行原理與個性化登入實現

正如你可能知道的兩個應用程式的兩個主要區域是“認證”和“授權”(或者訪問控制)。這兩個主要區域是Spring Security的兩個目標。“認證”,是建立一個他宣告的主題的過程(一個“主體”一般是指使用者,裝置或一些可以在你的應用程式中執行動作的其他系統)

基於spring-security-oauth2實現單點登入持續更新

##基於spring-security-實現資料庫版## 文章程式碼地址:[連結描述][1]可以下載直接執行,基於springb

LCN基於Spring cloud2.0實現分散式事物管理LCN的修改和部署

首先,對專案進行編譯,裝好maven環境,jdk環境。命令如下,注意這裡需要jdk1.8以上 mvn install -Dmaven.test.skip=true 打包之後,將編譯好的jar包上傳到自己的私服。 獲取最新的tx-manager的jar

Java for Web學習筆記Spring security準備2授權

訪問的範圍和許可權屬於授權。 Principals和Identities 就Java而言,很方便利用java.security.Principal。Principal至少會包含已被認證的使用者identity,例如使用者名稱,還可能有其他資訊。此外還可以保護使用者的授權資