필요. 첫 번째는 스프링 보안 xml 구성이고 두 번째 부분은 스프링 보안이 제공하는 UserContextMapper를 재정의합니다.
보안 XML 구성은
<bean id="adAuthenticationProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="my.domain.com" />
<constructor-arg value="ldap://<adhostserver>:<port>/" />
<property name="convertSubErrorCodesToExceptions" value="true" />
<property name="userDetailsContextMapper" ref="myUserDetailsContextMapper" />
</bean>
<bean id="myUserDetailsContextMapper" class="com.mycompany.sme.workflow.controller.MyDbAuthorizationFetcher">
<property name="dataSource" ref="dataSource" />
MyDbAuthorizationFetcher 당신이 DB
에서
public class MyDbAuthorizationFetcher implements UserDetailsContextMapper {
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSource dataSource;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
// populating roles assigned to the user from AUTHORITIES table in DB
private List<SimpleGrantedAuthority> loadRolesFromDatabase(String username) {
DbRole role = new DbRole();
String sql = "select * from user where user_id = ?";
jdbcTemplate = new JdbcTemplate(getDataSource());
role = jdbcTemplate.queryForObject(sql, new Object[] { username }, new DbRoleMapper());
try {
dataSource.getConnection().setAutoCommit(true);
} catch (SQLException e) {
}
List<SimpleGrantedAuthority> authoritiess = new ArrayList<SimpleGrantedAuthority>();
SimpleGrantedAuthority auth = new SimpleGrantedAuthority(String.valueOf(role.getRoleId()));
authoritiess.add(auth);
return authoritiess;
}
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx,
String username, Collection<? extends GrantedAuthority> authorities) {
List<SimpleGrantedAuthority> allAuthorities = new ArrayList<SimpleGrantedAuthority>();
for (GrantedAuthority auth : authorities) {
if (auth != null && !auth.getAuthority().isEmpty()) {
allAuthorities.add((SimpleGrantedAuthority) auth);
}
}
// add additional roles from the database table
allAuthorities.addAll(loadRolesFromDatabase(username));
return new User(username, "", true, true, true, true, allAuthorities);
}
@Override
public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
// TODO Auto-generated method stub
}
}
덕분에 많은 권한을 가져 UserContextMapper 클래스를 구현 될 것이다 클래스입니다 것입니다. 어떻게해야합니까? 이 기술로는 매우 익숙하며 사용자 정의 loadUserAuthorities를 사용하여 ActiveDirectoryLdapAuthenticationProvider를 사용하는 예제를 찾을 수 없습니다. –
클래스를 확장하고 데이터베이스를 호출하여 역할을 가져오고 메소드별로 'List'로 반환하십시오. 서명. 'SimpleGrantedAuthority' 클래스를 사용하여 개별 역할을 래핑 할 수 있습니다. –
확장하려고했지만 오류가 발생합니다. 최종 클래스를 서브 클래 싱 할 수 없습니다. ActiveDirectoryLdapAuthenticationProvider –