2011-10-24 10 views
1

私は春のセキュリティに新しいです。スプリングメソッドのセキュリティが私のために機能しません

私のアプリケーションでは春のセキュリティを使用しようとします。基本的なスプリングセキュリティ機能がうまく動作します。

しかし、特定の役割のみを許可する方法を保護しようとすると、保護されず、その方法がすべての役割に対して機能します。

ここで私は自分のコードスニペットを与えました。私はどこで間違ったのか分かりますか?

私は、このアプリケーションのために使用されるライブラリです

01.aopalliance-1.0.jar 
02.jcl-over-slf4j-1.6.1.jar 
03.jstl-1.2.jar 
04.logback-classic-0.9.29.jar 
05.logback-core-0.9.29.jar 
06.mysql-connector-java-5.1.12-bin.jar 
07.org.springframework.jdbc-3.1.0.RC1.jar 
08.org.springframework.transaction-3.1.0.RC1.jar 
09.slf4j-api-1.6.1.jar 
10.spring-aop-3.0.6.RELEASE.jar 
11.spring-asm-3.0.6.RELEASE.jar 
12.spring-beans-3.0.6.RELEASE.jar 
13.spring-context-3.0.6.RELEASE.jar 
14.spring-context-support-3.0.6.RELEASE.jar 
15.spring-core-3.0.6.RELEASE.jar 
16.spring-expression-3.0.6.RELEASE.jar 
17.spring-security-config-3.1.0.RC3.jar 
18.spring-security-core-3.1.0.RC3.jar 
19.spring-security-crypto-3.1.0.RC3.jar 
20.spring-security-taglibs-3.1.0.RC3.jar 
21.spring-security-web-3.1.0.RC3.jar 
22.spring-web-3.0.6.RELEASE.jar 
23.spring-webmvc-3.0.6.RELEASE.jar 

web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/security-app-context.xml 
      /WEB-INF/application-data-source.xml 
     </param-value> 
    </context-param> 

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

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <!-- 
     - Provides core MVC application controller. See bank-servlet.xml. 
    --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

ディスパッチャ-servlet.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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.1.xsd" 
     xmlns:ns1="http://www.springframework.org/schema/security"> 

    <ns1:global-method-security pre-post-annotations="enabled"/> 

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/pages/result/" 
      p:suffix=".jsp" > 
      <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.JstlView 
      </value> 
      </property> 
    </bean> 

    <!-- 
    The index controller. 
    --> 

    <bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
      p:viewName="index" /> 

    <bean name="/action.htm" id="action" class="com.spt3.controller.ActionController"> 
     <property name="methodNameResolver"> 
      <ref bean="paramResolver"/> 
     </property> 
    </bean> 

    <bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> 
     <property name="paramName"> 
      <value>action</value> 
     </property> 
    </bean> 

</beans> 

applicationContext.xmlを

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:security="http://www.springframework.org/schema/security" 
    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"> 


</beans> 

セキュリティ-APP-のcontext.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.1.xsd"> 

    <global-method-security pre-post-annotations="enabled"/> 

    <http use-expressions="true" auto-config="true" access-denied-page="/pages/accessDenied.jsp"> 
      <intercept-url pattern="/index.jsp" access="permitAll" /> 
      <intercept-url pattern="/**.htm" access="isAuthenticated()"/> 
      <intercept-url pattern="/pages/*" access="permitAll" /> 
      <intercept-url pattern="/user/*" access="isAuthenticated()"/>    
      <intercept-url pattern="/md/*" access="hasRole('MD')"/> 
      <intercept-url pattern="/admin/*" access="hasAnyRole('Administrator','MD')"/> 
      <intercept-url pattern="/manager/*" access="hasAnyRole('Manager','MD')"/> 
      <form-login login-page="/pages/login.jsp" authentication-failure-url="/pages/loginfailed.jsp"/> 
      <logout logout-success-url="/index.jsp"/> 
    </http> 

    <beans:bean id="encoder" class="com.spt3.encoder.MyPasswordEncoder"/> 

    <authentication-manager> 
     <authentication-provider> 
     <password-encoder ref="encoder"/> 
     <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, ur.authority from users u, user_roles ur where u.user_id = ur.user_id and u.username =? " 
     /> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

アプリケーションデータsource.xml

<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-3.0.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/app" /> 
     <property name="username" value="uname" /> 
     <property name="password" value="pword" /> 
    </bean> 

</beans> 

ActionController.java

package com.spt3.controller; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
import com.spt3.infc.ActionInterface; 
import org.springframework.security.access.prepost.PreAuthorize; 

public class ActionController extends MultiActionController implements ActionInterface { 

    public ActionController(){ 
    } 

    public ModelAndView getResult(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
     System.out.println(" - - - - - getResult() - - - - - "); 
     try{ 
      this.getPersonalInformation("MD"); 
     }catch(Exception e){ 
      System.out.println(" Exception : "+e); 
     } 
     return new ModelAndView("result"); 
    } 

    public void getPersonalInformation(String role){ 
     System.out.println(" "+role+"'s-Personal Information."); 
    } 
} 

ActionInterface.java

package com.spt3.infc; 

import org.springframework.security.access.prepost.PreAuthorize; 
public interface ActionInterface { 

    @PreAuthorize("hasRole('MD')") // Secured method only for role MD 
    public void getPersonalInformation(String role); 
} 

url( http://127.0.0.1:8080/myapp/action.htm?action=getResult)からgetResult()メソッドにアクセスすると、セキュリティで保護されたメソッドを呼び出すことができます。

マイ期待される結果

  • getPersonalInformation()メソッドは、MD
  • のみユーザロールの

にアクセスできなければなりません今実際の結果

  • getPersonalInformation()メソッドでありますすべてのユーザー役割を呼び出しています。豆を使用した後

- 編集豆の注入を使用している場合(1)ここで

同じ問題が発生しました。

ここで私はコードスニペットでファイルを変更しました。

ActionController.javaは

package com.spt3.controller; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
import com.spt3.infc.ActionInterface; 

public class ActionController extends MultiActionController { 

    private ActionInterface actionInterface; 

    public ActionController(){ 
    } 

    public ActionController(ActionInterface actionInterface){ 
     this.actionInterface=actionInterface; 
    } 

    public ModelAndView getResult(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
     System.out.println(" - - - - - getResult() - - - - - "); 
     try{ 
      actionInterface.getPersonalInformation("MD"); 
     }catch(Exception e){ 
      System.out.println(" Exception : "+e); 
     } 
     return new ModelAndView("result"); 
    } 

} 

ActionInterfaceのようなものです。ActionBean.java

package com.spt3.bean; 


import com.spt3.infc.ActionInterface; 
import org.springframework.security.access.annotation.Secured; 

public class ActionBean implements ActionInterface{ 

    @Secured("MD") 
    public void getPersonalInformation(String role){ 
     System.out.println(" "+role+"'s-Personal Information."); 
    } 

} 

と呼ばれるJavaの

package com.spt3.infc; 

public interface ActionInterface { 

    public void getPersonalInformation(String role); 
} 

Beanクラスiがオブジェクトを注入するコンストラクタインジェクションを使用。私は間違いをした

<bean id="actionBean" class="com.spt3.bean.ActionBean"/> <!-- Bean class --> 
<bean name="/action.htm" id="action" class="com.spt3.controller.ActionController"> 
     <property name="methodNameResolver"> 
      <ref bean="paramResolver"/> 
     </property> 
     <constructor-arg index="0" ref="actionBean"/> <!-- Injecting object to controller --> 
    </bean> 

。私に解決策を教えてください。

問題は解決しました。

これは以下の手順です。適切なxmlファイル内

  1. 設定豆
  2. 私は豆の注入を使用していました。
  3. 私は@PreAuthorizeアノテーションを使用しました。

最後に、プログラムが正常に実行されたときのクラスは次のようになります。

ActionBeanは、私が作成した別のクラスで、ActionInterface.javaクラスを実装し、getPersonalInformation()メソッドのメソッドを実装しました。

ActionInterface.javaこの

package com.spt3.infc; 

public interface ActionInterface { 

    public void getPersonalInformation(String role); 
} 

のように見え、ActionBean.javaこの

package com.spt3.bean; 


import com.spt3.infc.ActionInterface; 
import org.springframework.security.access.prepost.PreAuthorize; 

public class ActionBean implements ActionInterface{ 

    @PreAuthorize("hasRole('MD')") 
    public void getPersonalInformation(String role){ 
     System.out.println(" "+role+"'s-Personal Information."); 
    } 

} 

ActionController.javaのように見えるこの

package com.spt3.controller; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
import com.spt3.infc.ActionInterface; 

public class ActionController extends MultiActionController { 

    private ActionInterface actionInterface; 

    public ActionController(){ 
    } 

    public ActionController(ActionInterface actionInterface){ 
     this.actionInterface=actionInterface; 
    } 

    public ModelAndView getResult(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
     System.out.println(" - - - - - getResult() - - - - - "); 
     try{ 
      actionInterface.getPersonalInformation("MD"); 
     }catch(Exception e){ 
      System.out.println(" Exception : "+e); 
     } 
     return new ModelAndView("result"); 
    } 

} 

幸運のように見えるが... ありがとうございました。

答えて

3

春のセキュリティは、豆のメソッドへの呼び出しを傍受する春の豆の周囲のプロキシを使用して動作し、ユーザーに適切な役割がない場合は例外をスローします。しかし、コントローラはSpring Beanメソッドを呼び出さない。インスタンスメソッド:this.getPersonalInformation()を呼び出します。

これを行うと、別のSpring Beanメソッドが呼び出されないため、Springは呼び出しを傍受することができず、ユーザーに適切なロールがあることを確認できません。

getPersonalInformationメソッドを別のSpring Beanに入れて、このBeanをコントローラに挿入します。すべてが正常であるはずです。

サイドノート:そのような場合には、単に@Secured("MD")アノテーションを使用してください。 getResultメソッドにアノテーションを直接置くのはなぜですか?

+0

@JB_Nizet問題は今解決されました。実際には、メソッドを保護するためにBeanの設定が必要です。 @Secured( "MD")を@PreAuthrize( "hasRole( 'MD')"に変更しました)。変更後は正常に動作し、メソッドは保護されます。他のロールがメソッドにアクセスしようとしたときに例外を出しています...ありがとうございます。 – Mohan