2016-09-22 94 views
-1

我對spring security非常陌生,所以請耐心等待。如果有人能指導我,我願意提出這個問題更具體的建議。Spring安全攔截URL不能與自定義的UserDetails對象一起使用

我的問題是我在Spring安全中有一個攔截URL配置,但即使用戶具有必需的角色,它總是重定向到訪問被拒絕的頁面。這是我的春節,安全配置:

<?xml version="1.0" encoding="UTF-8"?> 
<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-4.1.xsd"> 

    <!-- enable use-expressions --> 
    <http auto-config="true" use-expressions="true"> 

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

     <!-- access denied page --> 
     <access-denied-handler error-page="/403" /> 

     <session-management invalid-session-url="/login" 
      session-fixation-protection="newSession"> 
      <concurrency-control max-sessions="1" 
       error-if-maximum-exceeded="true" /> 
     </session-management> 

     <form-login login-page="/login" authentication-failure-url="/login?error" 
      username-parameter="emailId" password-parameter="pwd" /> 
     <logout logout-success-url="/login?logout" delete-cookies="JSESSIONID" /> 
     <csrf token-repository-ref="tokenRepository" /> 
    </http> 

    <authentication-manager> 
     <authentication-provider ref="customAuthenticationProvider" /> 
    </authentication-manager> 

</beans:beans> 

通過我的研究,我認爲有什麼錯在上面的配置,但它可能是一個問題,因爲定製的UserDetails對象,我使用。這是POJO:

public class CustomUser implements UserDetails { 

    private static final long serialVersionUID = 1L; 
    private String userID; 
    private String emailId; 
    private String password; 
    private boolean enabled = true; 
    private boolean accountNonExpired = true; 
    private boolean credentialsNonExpired = true; 
    private boolean accountNonLocked = true; 
    private List<Role> authorities; 

    @Override 
    public List<Role> getAuthorities() { 
     return authorities; 
    } 
    //other setters and getters 
} 

角色類:

public class Role implements GrantedAuthority { 

    private static final long serialVersionUID = 1L; 
    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAuthority() { 
     return this.name; 
    } 
} 

我也有填充CustomUser POJO定製的UserDAO類,我已經證實沒有價值觀的設置沒有問題。

這是我的原則(如寫在日誌):

Principal: CustomUser [userID=user1, [email protected], password=pwd, enabled=true, accountNonExpired=true, credentialsNonExpired=true, authorities=[Role [name=ADMIN]]]; 

什麼可能是頁面總是被拒絕的原因是什麼?

感謝您抽出時間來閱讀這整個帖子:)

回答

1

改變

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

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

編輯

如果以前的解決方案不起作用,然後嘗試這種方式。

查看自己的角色,它返回 「管理員」 和你期望 「ROLE_ADMIN」

改變角色名稱爲表

「ADMIN」 到 「ROLE_ADMIN」

+0

「ROLE_」 是一個預定義Spring安全性添加到所有角色AFAIK的前綴。但是,我確實嘗試了您的建議,但沒有幫助。問題仍然存在。 – javaGirl243

+0

看看這個:[Spring Security增加一個前綴](http://stackoverflow.com/a/33206127/6345100) – javaGirl243

+0

請檢查我編輯的答案。 –