私はまだ春には特に春のセキュリティで新しいです。このアプリケーションは安らかなアプリケーションです。続き@RestControllerで春のセキュリティ - JSONish CustomAuthenticationProviderの応答
は@RestController
からの抜粋です:
@RequestMapping(value = "/new", method = RequestMethod.POST)
@PreRegistration("new")
@ResponseBody
public ResponseEntity<Void> newUser(@RequestBody @Valid TempUser user, UriComponentsBuilder ucBuilder) {
registerService.addUser(user);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/register/{userName}").buildAndExpand(user.getUserName()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
CustomAuthenticationProvider
からの抜粋を以下に示します。
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
final String name = authentication.getName();
final String password = authentication.getCredentials().toString();
if (name.equals("admin") && password.equals("system")) {
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
final UserDetails principal = new User(name, password, grantedAuths);
final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
return auth;
}
else {
throw new BadCredentialsException("NOT_AUTHORIZED");
}
}
SecurityConfig:CustomAuthenticationProvider
が投げたときに
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestCache()
.requestCache(new NullRequestCache())
.and()
.httpBasic()
.and()
.csrf().disable();
}
私が達成しようと何があります例外(例:「bad cr 「完全な認証が必要です...」という応答をカスタマイズし、レスポンスボディにJSON形式で戻したいと考えています。
私が行ったことは、新しい例外を作成し、AOPを使用してそれを呼び出すことです。しかし、それは動作していないようです。私も@ControllerAdvice
を使用しようとしましたが、CustomAuthenticationProvider
がコントローラの外にあるので、うまく動作しないようです(おそらく)。
に読むことができます"addFilterAfter"を追加したとき、リクエストは永久に無効になります。ログインしても正しいです。何か案が? – lxnx