2017-01-20 6 views
4

現在、私はSpring MVCアプリケーションを開発中です。ログイン時にSpring Securityログインユーザにカスタムフィールドを追加する必要があります(ユーザ名、パスワードを挿入します)。 、カスタム値)。 この値は、ユーザーがログインしているとき(pricipal.getValueなど)どこでも使用できる必要があります。SPRING:セキュリティユーザにカスタムユーザの追加

は、私は、カスタム・ユーザー・クラスとカスタムサービスについて多くを読んで、本当に

すべてのヘルプは素晴らしいことだ...私の問題のために働いて解決策を見つけることができません!

答えて

8

カスタムを使用します。UserdetailsService

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
@Autowired 
@Qualifier("userDetailsService") 
UserDetailsService userDetailsService; 

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

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    //authorization logic here ... 
} 

    @Bean 
    public PasswordEncoder passwordEncoder(){ 
     // return preferred PasswordEncoder ...// 
    } 


} 

ここでは、カスタムのサンプルUserDetails実装: custom UserDetails

+1

私の答えはなぜダウン投票されたのですか? –

3

UserDetailsインターフェイスを実装するクラスを作成します。

public class User implements UserDetails { 
    // Your user properties 
    // implement methods 
} 

一度認証されると、このようなプロジェクトのどこにでもこのオブジェクトにアクセスできます。

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

    //get user from the database, via Hibernate 
    @Autowired 
    private UserDao userDao; 

    @Transactional(readOnly=true) 
    @Override 
    public UserDetails loadUserByUsername(final String username) 
     throws UsernameNotFoundException { 
//CUSTOM USER HERE vvv 
     User user = userDao.findByUserName(username); 
     List<GrantedAuthority> authorities = 
             buildUserAuthority(user.getUserRole()); 
//if you're implementing UserDetails you wouldn't need to call this method and instead return the User as it is 
     //return buildUserForAuthentication(user, authorities); 
return user; 

    } 

    // Converts user to spring.springframework.security.core.userdetails.User 
    private User buildUserForAuthentication(user, 
     List<GrantedAuthority> authorities) { 
     return new User(user.getUsername(), user.getPassword(), 
      user.isEnabled(), true, true, true, authorities); 
    } 

    private List<GrantedAuthority> buildUserAuthority(Set<UserRole> userRoles) { 

     Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>(); 

     // add user's authorities 
     for (UserRole userRole : userRoles) { 
      setAuths.add(new SimpleGrantedAuthority(userRole.getRole())); 
     } 

     List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths); 

     return Result; 
    } 

} 

そして、あなたは自分のWebConfigurerAdapterを設定:のAvinashはあなたUserクラスUserDetailsを実装することができますし、あなたもUserDetailsServiceを実装し、対応するメソッドをオーバーライドするカスタムUserオブジェクトを返すことができ、言ったと同じように

User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
+1

hm私は本当に理解していない...私は私のカスタム文字列フィールドでユーザークラスを作った。 mehtodsの束は、\t '@Override \tパブリック文字列getPasswordに(){ \t \t // TODO自動生成方法スタブ \t \t戻りヌルように生成されます。 \t} ' どのように私は春のセキュリティのための"ユーザー "クラスを使用できますか?またはそれはauomaticlyで動作しますか? 私は、コード例が大いに役立つと思います。/ – counter1602

+0

ここに完全な例がありますhttp://forum.spring.io/forum/spring-projects/security/122998-how-to-use-custom-userdetails-in -spring-security?p = 601011#post601011 – Avinash

関連する問題