【发布时间】:2016-09-29 09:21:18
【问题描述】:
我有提供用户信息的 OAuth2 授权服务器:
public class User implements Serializable, UserDetails {
private Long userID;
private String username;
private String password;
private String fullName;
private String email;
private String avatar;
private boolean enabled;
// etc
}
@RestController
@RequestMapping("/api")
public class APIController {
@RequestMapping("/me")
public User me(@AuthenticationPrincipal User activeUser) {
return activeUser;
}
}
我还实现了 OAuth2 客户端作为单独的 Spring Boot 应用程序。
@Configuration
@EnableOAuth2Sso
public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.logout()
.and()
.antMatcher("/**").authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated();
}
}
application.yml
security:
user:
password: none
oauth2:
client:
clientId: acme
clientSecret: acmepassword
accessTokenUri: http://localhost:9080/sso/oauth/token
userAuthorizationUri: http://localhost:9080/sso/oauth/authorize
resource:
userInfoUri: http://localhost:9080/sso/api/me
用户认证成功:
@Controller
public class MainController {
@RequestMapping(value = "/")
public String index(Principal principal) {
System.out.println(principal);
// org.springframework.security.oauth2.provider.OAuth2Authentication@c2e723e8: Principal: superadmin; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=<ADDRESS>, sessionId=<SESSION>, tokenType=bearertokenValue=<TOKEN>; Granted Authorities: {userRoleID=1, authority=ROLE_SUPERUSER}
OAuth2Authentication auth = (OAuth2Authentication) principal;
System.out.println(auth.getUserAuthentication().getDetails());
// {userID=1, username=superadmin, password=***, fullName=SuperUser, email=superadmin@example.org, avatar=null, enabled=true ...
return "index";
}
}
但我不明白如何在我的应用程序中使用提供的 OAuth2Authentication 对象。几乎没用。
当我尝试使用任何 Thymeleaf 安全标签时
<span sec:authentication="principal.fullName">Username</span>
<span sec:authentication="principal.authorities">Authorities</span>
<span sec:authentication="principal.userAuthentication.details.fullName">Usernames</span>
..出现以下异常:
Error retrieving value for property "property name here" of authentication object of class org.springframework.security.oauth2.provider.OAuth2Authentication
标准 Spring Security 方法 isUserInRole() 也不起作用:
System.out.println(servletRequest.isUserInRole("ROLE_SUPERUSER"));
// false
我应该实现自定义 Thymeleaf 安全方言和 hasRole() 方法吗?或者可能存在更简单的解决方案?
【问题讨论】:
-
principal(如authentication.getPrincipal())通常是String,所以我怀疑它有不同的属性。 -
@M.感谢您的评论!找到了! Thymeleaf 标签不起作用,因为方言操作的 security.core.Authentication 对象不包含
UserAuthentication字段(这是我的自定义属性存储的地方)。而且我相信isUserInRole()方法不起作用,因为我使用了自定义GrantedAuthority对象。所以我只需要将Principal替换为User并将权限转换为适当的集合。
标签: java spring-mvc spring-security spring-boot spring-security-oauth2