這很容易給力的OAuth2,你只需要自己看着辦吧第一個:
@Autowired
private AuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private AccessDeniedHandler accessDeniedHandler;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.access("#oauth2.hasScope('read')")
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
}
然後你會n EED創建您的AuthenticationEntryPoint和accessDeniedHandler @Bean
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return new AccessDeniedHandler() {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
response.getWriter().append("\"FORBIDDEN\"");
response.setStatus(HttpStatus.FORBIDDEN.value());
}
};
}
@Bean
public AuthenticationEntryPoint authenticationEntryPoint() {
return new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.getWriter().append("\"UNAUTHORIZED\"");
response.setStatus(HttpStatus.UNAUTHORIZED.value());
}
};
}
隨意在JSON你喜歡的方式進行轉換,我建議你傑克遜。
非常好...這工作。 –