2014-02-19 103 views
1

我正在調查很長一個問題,我有我的網絡應用程序管理員註冊和登錄使用傳統方法與用戶名和密碼和常規用戶註冊並使用他們的Facebook帳戶登錄。春季安全和春季社交在不同的登錄頁面

這兩個登錄表單位於不同的頁面;不過,我希望這兩個基於同一個應用程序的Spring Security。

我有一個與此相關的幾個問題:

1)我想,而另一個在不同的頁面設置用戶登錄社交形式的着陸頁。爲了做到這一點,我如下配置了HttpRequest,但着陸頁未啓動。

2)我應該配置兩個HttpSecurity,一個用於傳統登錄,一個用於社交登錄? (當前配置不起作用)

3)我想定義兩個權限,一個用於管理員ROLE_ADMIN,另一個用於常規用戶ROLE_USER。我如何爲使用他們的soaicl賬戶登錄的用戶定義一個ROLE?

這裏是我的HttpSecurity配置

@Configuration 
@EnableWebSecurity 
public class SecurityConfig{ 

@Autowired 
private ApplicationContext context; 

@Autowired 
private DataSource dataSource; 

@Autowired 
public void registerAuthentication(AuthenticationManagerBuilder auth) 
     throws Exception { 

    auth.jdbcAuthentication() 
      .dataSource(dataSource) 
      .usersByUsernameQuery(
        "select username, password, enabled from Account where username = ?") 
      .authoritiesByUsernameQuery(
        "select username, authority from Account where username = ?") 
      .passwordEncoder(passwordEncoder()); 

} 


@Bean 
public UserIdSource userIdSource() { 
    return new AuthenticationNameUserIdSource(); 
} 

@Bean 
public PasswordEncoder passwordEncoder() { 
    return NoOpPasswordEncoder.getInstance(); 
} 

@Bean 
public TextEncryptor textEncryptor() { 
    return Encryptors.noOpText(); 
} 

@Configuration 
@Order(2) 
public static class BizSecurityConfigurationAdapter extends 
     WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http 
    .formLogin() 
    .loginPage("/admin") 
    .loginProcessingUrl("/admin/authenticate") 
    .failureUrl("/admin?param.error=bad_credentials") 
    .defaultSuccessUrl("/admin/home") 
    .and() 
    .authorizeRequests() 
    .antMatchers("/","/admin", "/resources/**", 
    "/auth/**", "/signin/**", "/signup/**", "/disconnect/**").permitAll() 
    .antMatchers("/**").authenticated() 
    .antMatchers("/**").hasRole("ADMIN") 
    .and() 
    .rememberMe(); 
    } 

    @Bean 
     public SocialUserDetailsService socialUsersDetailService() { 
      return new SimpleSocialUsersDetailService(userDetailsService()); 
     } 
} 

@Configuration 
@Order(1) 
public static class UserSecurityConfigurerAdapter extends 
     WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.formLogin() 
       .loginPage("/") 
       .loginProcessingUrl("/authenticate") 
       .failureUrl("/?param.error=bad_credentials") 
       .defaultSuccessUrl("/user/profile") 
       .permitAll() 
       .and() 
       .logout() 
       .logoutUrl("/") 
       .deleteCookies("JSESSIONID") 
       .and() 
       .authorizeRequests() 
       .antMatchers("/", "/user","/admin","/login", "/admin/**", 
         "/resources/**", "/auth/**", "/signin/**", 
         "/signup/**", "/disconnect/**").permitAll() 
       .antMatchers("/user/**").hasRole("USER").and().rememberMe() 
       .and().apply(new SpringSocialConfigurer()); 
    } 

    @Bean 
    public SocialUserDetailsService socialUsersDetailService() { 
     return new SimpleSocialUsersDetailService(userDetailsService()); 
    } 
} 

} 

回答

1

看來你是混合一些春天的安全/ OpenID的/ OAuth的 - 春天的社會觀念。

  1. 您不必爲Facebook登錄提供任何自定義表單。它由Facebook提供。您可以使用ProviderSignInController(默認實現)

  2. 嘗試在開始時配置具有spring安全性的admin登錄。然後混合在Facebook中。 loginPage方法自定義URL,它將處理POST請求的登錄,所以你的表單需要使用它作爲'action'參數。還要記住,spring security 3.2默認啓用了CSRF,所以你還必須通過隱藏輸入來傳遞。

  3. ProviderSignInController使用SimpleSignInAdapter來處理登錄。您可以在其signIn方法中設置角色並以編程方式在spring security中以facebook用戶身份登錄。喜歡的東西:

    private class SimpleSignInAdapter implements SignInAdapter { 
    public String signIn(String userId, Connection<?> connection, NativeWebRequest request) { 
        UserProfile userProfile = connection.fetchUserProfile(); 
        User user = userRepository.findByUsername(userProfile.getEmail()); 
        Collection<GrantedAuthority> authorities = new ArrayList<>(); 
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
        Authentication auth = new UsernamePasswordAuthenticationToken(user, null, authorities); 
        SecurityContextHolder.getContext().setAuthentication(auth); 
        return null; 
    } 
    } 
    
+0

感謝您的回答。我讀到了另一種通過使用過濾器SpringSocialConfigurer實現此方法的方法。如何在這種情況下設置角色,或者我不需要它? –