Spring Securityの概要

親愛なるhabrayuzer、Spring Securityの次のような側面を検討することをお勧めします。







Spring Securityは、Spring Frameworkを使用して作成されたエンタープライズアプリケーション向けの認証および承認システムやその他のセキュリティ機能を構築するためのメカニズムを提供するJava / JavaEEフレームワークです。 このプロジェクトは2003年後半にベンアレックスによって「Acegi Security」という名前で開始されました。最初のリリースは2004年にリリースされました。 その後、このプロジェクトはSpringに吸収され、公式の子会社プロジェクトになりました。 2008年4月に新しいSpring Security 2.0.0という名前で初めて公開されました。



主要なSpring Securityコンテキストオブジェクト:









認証





(1)ユーザーは、名前(ログインまたは電子メール)とパスワードを入力して、ログインするように求められます。 ユーザー名とパスワードは、UsernamePasswordAuthenticationTokenクラスのインスタンス(認証インターフェイスのインスタンス)に結合され、その後、検証のためにAuthenticationManagerインスタンスに渡されます。



(2)パスワードがユーザー名と一致しない場合、メッセージ「Bad Credentials」とともにBadCredentialsExceptionがスローされます。



(3)認証が成功すると、完全に入力された認証インスタンスが返されます。



(4)SecurityContextHolder.getContext()。SetAuthentication(...)メソッドを呼び出すことにより、ユーザーに対してセキュリティコンテキストが設定され、AuthenticationManagerが返したオブジェクトが渡されます。



SpringFrameworkアプリケーションのセキュリティサポート接続:





1. pom.xml



 <properties> <spring.version>3.1.4.RELEASE</spring.version> </properties> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.version}</version> </dependency>
      
      







2. web.xml



 <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>
      
      









そしてもちろん、 security.xml構成ファイル自体



 <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.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http access-denied-page="/error403.jsp"> <intercept-url pattern="/index*" access="ROLE_USER,ROLE_ANONYMOUS"/> <intercept-url pattern="/add*" access="ROLE_USER"/> <intercept-url pattern="/delete/*" access="ROLE_ADMIN"/> <form-login login-page="/login.jsp" default-target-url="/index" authentication-failure-url="/login.jsp?error=true"/> <logout logout-url="/logout" logout-success-url="/index"/> <anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/> <remember-me/> </http> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="pass" authorities="ROLE_ADMIN,ROLE_USER"/> <user name="user1" password="1111" authorities="ROLE_USER"/> <user name="user2" password="2222" disabled="true" authorities="ROLE_USER"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
      
      







security.xmlコードの説明:

ページlogin.jspにはaction = "/ j_spring_security_check"のフォームがあり、name = "j_username"およびname = "j_password"の名前とパスワードの入力、およびname = "_ spring_security_remember_me"のチェックボックスが含まれていることがわかります。 これらはすべて、Spring Securityが必要とする特別な値です。それ以外の場合、パラメーターはセキュリティコンテキストに渡されません。 認証に成功すると、ユーザーは承認ルールが既に適用されている/インデックスページにリダイレクトされます。 http spring-securityでフォームとURLを指定しない場合、デフォルトで基本認証が機能するか、httpでhttp spring-security <http-basic />を強制することで基本認証を有効にできます。



url / index *へのアクセスは、ROLE_USERの権利を持つユーザーとゲスト(認証されていないすべての接続がguestという名前とROLE_ANONYMOUSの権利を受け取る)に付与できます。

url / addへのアクセス* ROLE_USER特権を持つユーザーのみがurl / deleteへのアクセス* ROLE_ADMIN特権を持つユーザーのみ



また、許可されたアクセスをメソッドに固定することができます。これには、 security.xmlに次の要素を追加する必要があります。

 <global-method-security secured-annotations="enabled" />
      
      





そして、コード自体で:

  public interface AdminService { @Secured("ROLE_ADMIN") public Account editAccount(Account account); }
      
      





この例では、ユーザーは.xmlファイルのリストに保存されています。 UsernamePasswordAuthenticationTokenは、このデータと比較されます。 (ORMを使用して)データベース内のユーザーと比較するには、UserDetailsS​​erviceインターフェースのloadUserByUsernameメソッドを実装し、<authentication-provider user-service-ref = "userDetailsS​​ervice">でUserDetailsS​​ervice実装のBeanへの参照を指定する必要があります。 ORMを使用しておらず、JDBCを使用してデータベースからユーザー権限を抽出する必要がある場合は、Springがデータベースにアクセスする方法を知っているDataSource Beanを決定し、<authentication-provider>でこのBeanへのリンクを指定して2つ定義する必要があります必要なデータuser-by-username-queryおよびauthoritys-by-username-queryがプルされるクエリ。



 <beans xmlns="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-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/<yourDataBaseName>" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username, password, enabled from users where username = ?" authorities-by-username-query="select u.username, au.authority from users u, authorities au where u.id = au.user_id and u.username = ?" /> </authentication-provider> </beans>
      
      







ハッシュ化されたパスワードも確認できます。



 <authentication-manager> <authentication-provider> <password-encoder hash="sha"/> <user-service> <user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> <password-encoder hash="sha"> <salt-source user-property="username"/> </password-encoder>
      
      







それだけです 詳細はこちら:

(1) ドキュメント

(2) 応用例



All Articles