JEE 컨테이너는 일반적으로 내부 배포 역할을 내부 사용자 역할에 매핑하기 위해 독점 배포 설명자를 사용하는 메커니즘을 제공합니다. 즉, 애플리케이션은 web.xml의 내부 역할을 선언하고 사용하며 사용자에게 할당 된 실제 역할을 내부 역할에 매핑하는 파일 (예 : weblogic.xml)이 있습니다.스프링 보안 역할 할당
스프링 보안을 사용할 때 어떻게 그러한 매핑을 할 수 있습니까? 스프링 보안 3.0.x를 사용하고 있습니다.
JEE 컨테이너는 일반적으로 내부 배포 역할을 내부 사용자 역할에 매핑하기 위해 독점 배포 설명자를 사용하는 메커니즘을 제공합니다. 즉, 애플리케이션은 web.xml의 내부 역할을 선언하고 사용하며 사용자에게 할당 된 실제 역할을 내부 역할에 매핑하는 파일 (예 : weblogic.xml)이 있습니다.스프링 보안 역할 할당
스프링 보안을 사용할 때 어떻게 그러한 매핑을 할 수 있습니까? 스프링 보안 3.0.x를 사용하고 있습니다.
봄 보안 3.0.x. 그러한 매핑을 제공하지 않습니다.
그러나 인증 방법에 사용되는 인증 공급자를 확장하여 직접 구현할 수 있습니다.
DaoAuthenticationProvider
(내부 번호는 UserDetailsService
)을 사용하는 경우 addCustomAuthorities(String username, List<GrantedAuthority> authorities)
메서드를 재정 의하여 이미 부여 된 역할에 따라 새/매핑 된 역할을 추가 할 수 있습니다. 예를 들어
UserDetailsService
확장 : 역할을 매핑 할 수있는 YourMappingService
를 사용
...
@Override
protected void addCustomAuthorities(String username, List<GrantedAuthority> authorities) {
super.addCustomAuthorities(username, authorities);
List<GrantedAuthority> additional = new ArrayList<GrantedAuthority>();
for (GrantedAuthority role : authorities) {
additional .addAll(vourMappingService.getAdditionalForRole(role));
}
authorities.addAll(additional);
}
(새로운 역할을 추가하여 한 번 기존)
public class YourMappingService
/**
* Property bases mapping of roles to privileges.
* Every role is one line, the privileges are comma separated.
*/
private Properties roleToPrivileges;
public YourMappingService(Properties roleToPrivileges) {
if (roleToPrivileges == null) {
throw new IllegalArgumentException("roleToPrivileges must not be null");
}
this.roleToPrivileges = roleToPrivileges;
}
@Override
public Collection<? extends GrantedAuthority> getAdditionalForRole(GrantedAuthority role) {
String authority = role.getAuthority();
if(authority != null) {
String commaSeparatedPrivileges = roleToPrivileges.getProperty(role.getAuthority());
if (commaSeparatedPrivileges != null) {
List<GrantedAuthority> privileges = new ArrayList<GrantedAuthority>();
for(String privilegeName : StringUtils.commaDelimitedListToSet(commaSeparatedPrivileges)) {
privileges.add(new GrantedAuthorityImpl(privilegeName.trim()));
}
return privileges;
} else {
return Collections.emptyList();
}
} else {
return Collections.emptyList();
}
}
}
구성 :
<bean id="myUserDetailsService" class="de.humanfork.springsecurityroles.impl.JdbcDaoPrivilegesImpl">
<constructor-arg ref="yourMappingService"/>
<property name="dataSource" ref="dataSource"/>
<property name="usersByUsernameQuery" value="SELECT login,encryptedPassword,loginEnabled FROM user WHERE login = ?"/>
<property name="enableAuthorities" value="true"/>
<property name="authoritiesByUsernameQuery" value="SELECT u.login, r.securityRoles FROM user u, user2security_roles r WHERE u.login= ? AND u.id = r. User_fk;"/>
</bean>
<bean id="yourMappingService" class="ZourMappingService">
<constructor-arg>
<props>
<prop key="ROLE_ADMIN">
ROLE_backend_access,
ROLE_user_mngt,
ROLE_passwordLostRequest_mngt,
ROLE_log_mngt
</prop>
<prop key="ROLE_USER">
</prop>
</props>
</constructor-arg>
</bean>