私は、データベースとLdapを介してユーザを認証するためにSpringセキュリティとアノテーションを使用しています。 詳細につきましては、Ldapは属性取得を許可していないため、Ldap検索でユーザー(一意のコード)とパスワードが正しいかどうかを確認し、データベースを使用して権限を読み込みます。だから私のデータベースにすべてのユーザーがLdapに存在しますが、ユーザーがLdapに存在し、私のデータベースに存在しない場合は、特定のページを表示します。 これは、実際のコードです:私はシンプルなユーザーを持っていると私は名前/姓と電子メールのようないくつかの情報を追加したいと思いカスタムユーザのデータベースによるSpringセキュリティ認証
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
@PropertySource(value = { "classpath:application.properties" })
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationConfiguration authenticationConfiguration;
@Configuration
protected static class AuthenticationConfiguration implements
AuthenticationProvider {
@Autowired
private UserServices userServices;
@Autowired
LdapServices ldapServices;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
String name = authentication.getName();
String password = authentication.getCredentials().toString();
boolean isFind = ldapServices.ldapSearch(name, password);
if (isFind){
com.domain.User user = userServices.getByUsersEnabled(name);
if (user!=null)
authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));
return new UsernamePasswordAuthenticationToken(name, password, authorities);
}
else return null;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationConfiguration);
}
...web services authentication
。私はUserDetails
とloadUserByUsername
のUserDetailsService
インターフェイスを実装する必要があることを読んだが、私はloadUserByUsername
を自分のコードにマージできますか?このようにして、ユーザーコードの代わりに名前と姓を表示することができます。私も同様の問題のいくつかの問題を抱えていた、そして私は私がそれを得るために助けた偉大な記事を見つけたおかげ
このリンクは質問に答えるかもしれませんが、ここでは回答の重要な部分を含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューより](/レビュー/低品質の投稿/ 17660666) –
こんにちは@ProkashSarkar、あなたの返信をありがとう。私は答えを編集し、いくつかのコードを追加しました。私は今それが良いと思う。 –