2017-02-28 11 views
0

私のアプリケーションには2つのロールがあり、1つのロールはゲストのデータを表示し、もう1つのロールはadminです。管理ページでは、管理者はデータを編集でき、データを表示する。 URLにアクセスしようとすると、ゲストと管理者の両方のページビューと管理者が表示されますが、管理者ページにアクセスしないようにしたいと思います。春のセキュリティで特定のユーザーの特定のURLにアクセスする

次は私の春のセキュリティファイルです:

<b:beans xmlns="http://www.springframework.org/schema/security" 
     xmlns:b="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 auto-config="true" use-expressions="true"> 
     <!-- Adds Support for basic authentication --> 
     <intercept-url pattern="/admin" access="hasAnyRole('ROLE_USER')" /> 
     <!-- <http-basic /> --> 
     <form-login login-page="/login" authentication-failure-url="/loginFailed" default-target-url="/view" /> 
     <logout /> 
    </http> 
    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="leader" password="1234" authorities="ROLE_ADMIN" /> 
       <user name="sudheer" password="1234" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 
</b:beans> 

そして、次は私のコントローラクラスである:

@RequestMapping(value="/admin", method=RequestMethod.GET) 
    public ModelAndView admin(){ 

      ModelAndView model = new ModelAndView(); 

      List<ApplicationTO> list=application.getApplicationList(); 
      model.addObject("applicationList", list); 
      model.setViewName("admin"); 

      return model; 

     } 

    @RequestMapping(value="/view", method=RequestMethod.GET) 
    public ModelAndView view(){ 

      ModelAndView model = new ModelAndView(); 
      List<ApplicationTO> list=application.getApplicationList(); 
      model.addObject("applicationList", list); 
      model.setViewName("view"); 

      return model; 

     } 

答えて

2

変更してください - <intercept-url pattern="/admin" access="hasAnyRole('ROLE_USER')" />をあなたのコードから

<intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /> 
関連する問題