0
My appは、JSONインターフェイスのみ(Webページなし)を提供する完全なRESTfulスタイルのWebアプリケーションです。私は、私は完全にログインプロセスを制御することができ、自分のコントローラ書きたい:自分のコントローラを使用してログインするには?
@GetMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password,
HttpServletRequest req,
HttpServletResponse resp) {
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(username, password);
try {
Authentication auth = authManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(auth);
} catch (AuthenticationException ex) {
return ex.getMessage();
}
return "success";
をそして、これは私のUserDetailsService
次のとおりです。
public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
if (!name.equals("whf")) {
throw new UsernameNotFoundException("not found");
}
// load roles
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("admin"));
return new User("whf", "pwd", authorities);
}
}
しかし、私はadmin
役割を必要とURLにアクセスすると、私はAccess is Denied
を得ました:
@GetMapping("/a")
@PreAuthorize("hasRole('admin')")
public String A(Principal principal) {
return "a";
}
現在のユーザーにはadmin
ロールが付与されています。 /a
にアクセスできないのはなぜですか?