我的要求是使用基於SAML的SSO。從SAML斷言中檢索用戶組,並確保api端點的其餘部分安全。我正在使用Spring安全性SAML擴展和Spring MVC。我採取的步驟是。Spring Security SAML擴展和@PreAuthorize
- 使用Spring SAML擴展配置SP應用程序。 [完成]
- 檢索斷言並分配角色[完成]
- 創建剩餘端點。 [完成]
- 基於角色的安全休息端點和服務。 [不工作]
我已經實現SAMLUserDetailsService,它返回一個UserDetails對象的權限。 'loadUserBySAML'如下。
@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
final String userId = credential.getNameID().getValue();
final String emailAddress = credential.getAttributeAsString("EmailAddress");
final String firstName = credential.getAttributeAsString("FirstName");
final String lastName = credential.getAttributeAsString("LastName");
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT"));
return new User(userId, emailAddress, firstName, lastName, authorities);
}
我已將<!-- Enable security annotations on methods --> <security:global-method-security pre-post-annotations="enabled" />
添加到securityContext.xml中。
在RestController和服務上我使用@PreAuthorize,但是這個註解似乎根本沒有效果。
@PreAuthorize("hasRole('ROLE_PROGRAMLEAD')")
@RequestMapping(method = RequestMethod.GET)
public String hello() {
return "Hello.";}
有人可以幫我理解爲什麼PreAuthorize沒有發射嗎?我是否缺少一些配置?