尽管有正确的凭据,但Spring安全仍在给予

时间:2016-07-15 10:59:04

标签: java spring spring-security

嗨我从春天开始被拒绝访问。有人可以帮我这个

我正在使用spring 4.3

我的控制器

@RestController
@RequestMapping (value = "/api/secured/resource/school")
@Secured ({ ApplicationConstants.Role.SYSTEM_ADMIN, ApplicationConstants.Role.SCHOOL_ADMIN })
public class SchoolController
{

    @Autowired
    private SchoolService schoolService;

    @PostMapping (consumes = "application/json")
    @Secured ({ ApplicationConstants.Role.SYSTEM_ADMIN })
    public @ResponseBody ResponsePayload createSchool (HttpServletRequest request, @RequestBody School school)
            throws ServiceException
{
}

 interface Role
    {

        String SYSTEM_ADMIN = "SYSTEM_ADMIN";

        String SCHOOL_ADMIN = "SCHOOL_ADMIN";
    }

如下面的日志中所示,您可以看到用户有两个权限

SYSTEM_USER,SYSTEM_ADMIN仍然给它拒绝访问

Spring配置

<http pattern="/static/**" security="none" />
    <http use-expressions="true">
        <intercept-url pattern="/app/**" access="isAuthenticated()" /> 
        <form-login login-page="/loginPage"
            authentication-success-handler-ref="mySuccessHandler"
            authentication-failure-handler-ref="myFailureHandler" />
        <logout logout-success-url="/loginPage" />
        <custom-filter ref="loginFilter" after="FIRST" />
        <csrf disabled="true" />
    </http>

2016-07-15 16:03:12,525 DEBUG MethodSecurityInterceptor:348 - 以前经过身份验证:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@7670236f:Principal:SystemUser [userId = 1,name = Administrator,email = admin @ tepachi .com,mobilePhone = 9999999999,status = ACTIVE];证书:[保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@0:RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId:1h5x6yxtd1m0y1ogs4h5vfo1yl; 授权机构:SYSTEM_USER,SYSTEM_ADMIN 2016-07-15 16:03:12,525 DEBUG AffirmativeBased:66 - 选民:org.springframework.security.access.vote.RoleVoter@55951fcd,返回:0 2016-07-15 16:03:12,527 DEBUG AffirmativeBased:66 - 选民:org.springframework.security.access.vote.AuthenticatedVoter@46ab4efc,返回:0 2016-07-15 16:03:12,529 DEBUG ExceptionHandlerExceptionResolver:133 - 解决处理程序中的异常[public com.tepachi.web.response.ResponsePayload com.tepachi.web.controller.SchoolController.createSchool(javax.servlet.http.HttpServletRequest, com.tepachi.db.entities.user.School)throws com.tepachi.exception.ServiceException]:org.springframework.security.access.AccessDeniedException:访问被拒绝

1 个答案:

答案 0 :(得分:1)

问题是从4月4日开始,它将ROLE_置于授予的权限之前。

  

hasRole([role]):   如果当前主体具有指定的角色,则返回true。默认情况下,如果提供的角色不是以&#39; ROLE _&#39;它会被添加。这可以通过修改DefaultWebSecurityExpressionHandler上的defaultRolePrefix来自定义。

可在此处找到更多信息Spring Doc