0

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)に基づいて、ログインする各ユーザにユーザロールを設定することです。

+0

はいこれはゼロです。リンクありがとう。私はそれを通過します。一方、この質問にいくつかの洞察を与えることができれば、それは素晴らしいことでしょう – SameeraR

答えて

-2

これは、Springのログイン成功ハンドラを使用して実行できます。コードの例の下に。

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .formLogin() 
       .loginProcessingUrl("/app/url") 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .successHandler(loginSuccessHandler) 
       .failureHandler(loginFailureHandler) 
       .permitAll() 
       .and() 
      .logout() 
       .logoutUrl("/app/logout") 
       .logoutSuccessHandler(ajaxLogoutSuccessHandler) 
       .deleteCookies("JSESSIONID") 
       .permitAll() 
       .and() 
      .csrf() 
       .disable() 
      .authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .antMatchers("/api/**").authenticated(); 
    } 

ログイン成功ハンドラ:あなたはApplicationDetailServiceで次のように行うことができます

@Component 
public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, 
      HttpServletResponse response, Authentication authentication) 
      throws IOException, ServletException { 
     User user =  (User)authentication.getPrincipal(); 
     String name = user.getUsername(); 
     //Your Code based on code and set role in Spring Security 
     response.setStatus(HttpServletResponse.SC_OK); 

    } 
} 
-1

第二の方法:

@Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) 
      throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(encryptDecrypt); 
    } 

ApplicatonDetailService:

@Service("userDetailsService") 
public class ApplicationUserDetailsService implements UserDetailsService { 

    @Transactional(readOnly = true) 
    public UserDetails loadUserByUsername(final String username) 
      throws UsernameNotFoundException { 

     com.your.User domainUser = userDAO.findByEmail(username); 

     List<GrantedAuthority> authList = getGrantedAuthorities(new SimpleGrantedAuthority("ROLE_ADMIN")); 
     return new User(domainUser.getEmail(), domainUser.getPassword(), 
      enabled, accountNonExpired, credentialsNonExpired, 
      accountNonLocked, authList); 

    } 
} 

あなたがベースの役割を設定することができますコンディで:

List<GrantedAuthority> authList = getGrantedAuthorities(new SimpleGrantedAuthority("ROLE_ADMIN")); 
関連する問題