春季安全特定用户的访问特定URL

问题描述:

我在我的应用程序中有两个角色,一个角色是供访客查看数据,另一个角色是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; 

     } 

请改变 - 从你的代码<intercept-url pattern="/admin" access="hasAnyRole('ROLE_USER')" />

<intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />