Spring Webアプリケーションに正常にログインしたユーザにロールを割り当てようとしています。私はこれを行うためにLDAP認証を使用しています。Springセキュリティでユーザがログインしたときにグループ名に基づいてロールを割り当てる方法
マイWebSecurityConfig.java
package com.logsniffer.web.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.context.annotation.Bean;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.core.env.Environment;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.core.context.SecurityContextHolder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
Environment env;
@Autowired
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth)
throws Exception
{
if ("h2".equals(env.getProperty("spring.profiles.active")))
return;
DefaultSpringSecurityContextSource context = new DefaultSpringSecurityContextSource("ldap://10.100.7.99:389/dc=ldap,dc=com");
context.afterPropertiesSet();
auth.ldapAuthentication()
.contextSource(context)
.userSearchFilter("uid={0}")
.userSearchBase("ou=support")
// .groupSearchBase("ou=support")
// .groupSearchFilter(String groupSearchFilter)
.groupRoleAttribute("cn")
.rolePrefix("");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
// .antMatchers("/", "/home").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
}
は、たぶん私はこの間違ったを理解しています。私は思った使用
.groupRoleAttribute("cn")
は、ユーザーの役割を設定します。しかし、私のようなユーザー権限を印刷しようとすると、
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String r= authentication.getAuthorities().toString();
log.info("$$$$$$$$$$ " + r);
私は空の文字列を取得します。マイLDAPツリーは私がユーザーを認証しようとしている方法が間違っている、registerGlobalAuthentication
方法でprettry確信して次のように構成
+--> dc=ldap,dc=com (1+)
---> cn=admin
---> ou=hr
+--> ou=support (1+)
| +--> cn=admin (2)
| | ---> cn=kaushan deva
| | ---> cn=sameera ramasinghe
| | ---> Create new entry here
| +--> cn=regular (1)
| | ---> cn=chamitha abe
を持っています。そのため、私はグループロールの属性を設定できません。しかし、私は非常にこれに新しいので、私はエラーを見つけることができません。
私がしたいのは、自分のグループ(またはregular
)に基づいて、ログインする各ユーザにユーザロールを設定することです。
はいこれはゼロです。リンクありがとう。私はそれを通過します。一方、この質問にいくつかの洞察を与えることができれば、それは素晴らしいことでしょう – SameeraR