Spring Initializer、埋め込みTomcat、Thymeleafテンプレートエンジン、およびパッケージを実行可能なJARファイルとして使用してSpringブートWebアプリケーションを生成しました。使用Springブート - inMemoryAuthenticationの使用
技術:
春ブーツ2.0.0.M6、Javaの8、Mavenの私は私のアプリでは、開発段階
にメモリ認証に使用するために、このセキュリティ設定ファイルを持っている
。私は、ユーザーのドメインオブジェクトがあります。私のセキュリティ設定クラスで
import org.springframework.security.core.userdetails.UserDetails;
public class User implements Serializable, UserDetails {
..
}
を
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser(User
.withDefaultPasswordEncoder()
.username(DEV_USER)
.password(DEV_PWD)
.roles("ADMIN").build());
}
しかし、私はSecurityContextHolderからユーザーを取得するとき:
:私はエラーを得たSecurityContextHolder.getContext().
getAuthentication().getPrincipal()
org.springframework.security.core.userdetails.User cannot be cast to com.iberia.domain.backend.User
しかし、 ユーザー==>org.springframework.security.core.userdetails.User
SimpleGrantedAuthority ==>org.springframework.security.core.authority.SimpleGrantedAuthority
その後:
UserDetails
.withDefaultPasswordEncoder()
.username(DEV_USER)
.password(DEV_PWD)
.roles("ADMIN").build()
あなたは試しました オブジェクトプリンシパル= SecurityContextHolder.getContext()。getAuthentication()。getPrincipal(); if(principal instanceof UserDetails) userName =((UserDetails)プリンシパル).getUsername(); ? – 3vge
[Springセキュリティを使用してInMemoryAuthentication経由でカスタムユーザをログに記録する方法](https://stackoverflow.com/questions/22564532/how-to-get-a-custom-user-logged-via-inmemoryauthentication- with-spring-security) – dur
カスタムユーザーを使用する必要がある場合は、他の質問に記載されているように、独自の 'UserDetailsService'を実装する必要があります。カスタムユーザが必要ない場合は、Spring Securityのユーザを使用できます。 – dur