데이터베이스와 Ldap을 통해 사용자를 인증하기 위해 스프링 보안과 주석을 사용하고 있습니다. 즉, Ldap은 속성 검색을 허용하지 않기 때문에 Ldap 검색을 통해 사용자 (고유 코드)와 비밀번호가 올바른지 확인한 다음 내 데이터베이스를 사용하여 권한을로드합니다. 그래서 내 데이터베이스에있는 모든 사용자가 Ldap에 존재하지만, 사용자가 내 데이터베이스가 아닌 Ldap에 존재하면 특정 페이지를 보여줍니다. 이 실제 코드 :커스텀 사용자 데이터베이스를 통한 스프링 보안 인증
@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, 답장을 보내 주셔서 감사합니다. 나는 대답을 편집하고 그것에 약간의 코드를 추가했다. 나는 그것이 지금 더 낫다고 생각한다. –