問題一:OAuth2Authentication的值為null
- 問題描述:
在代碼清單7-1中恬总,有以下這么一段代碼:
@RequestMapping(value = { "/user" }, produces = "application/json")
public Map<String, Object> user(OAuth2Authentication user) {
Map<String, Object> userInfo = new HashMap<>();
userInfo.put("user", user.getUserAuthentication().getPrincipal());
userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
return userInfo;
}
在將OAuth2訪問令牌作為首部去請求/user時,代碼中user的值一直為null。
- 原因:Spring Cloud的版本由1.4升到1.5時赋元,有關OAuth過濾器的order由3變?yōu)榱?,這導致了上述代碼中的user變?yōu)閚ull。
- 解決方案:
1.第一種方案是將Spring Cloud的版本降為1.4
2.第二種方案是在application.yml文件中爸黄,添加以下屬性:
security:
oauth2:
resource:
filter-order: 3