2015-11-16 12 views
8

Aynı REST hizmeti için temel kimlik doğrulaması ve form oturum açmanın bir yolu var mı? Oturum açmaya izin vermek istiyorum kullanıcı oturum açtıktan sonra web tarayıcısı aracılığıyla ve komut satırından çalışan curl -u username:password hostname.com/api/process Şimdi bu yazıyı gördüm: Basic and form based authentication with Spring security Javaconfig ama yapmaya çalıştığımdan biraz farklı. Bunu yay ile ayarlamak için bir yol var mı? Ne var şimdi şudur: Tek sorun hostname.com/index veya hostname.com/ yerine temel kimlik doğrulama bilgileri soran pencere pop up çağrıldığında benim giriş sayfasına yönlendirir olmamasıdırAynı REST Api için temel kimlik doğrulamayı ve form oturum açma özelliğini birleştirin

package com.my.company.my.app.security; 

import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
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.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.security.crypto.password.PasswordEncoder; 
import org.springframework.security.provisioning.JdbcUserDetailsManager; 

import javax.sql.DataSource; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SecurityConfig.class); 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .antMatchers("/js/**", "/css/**") 
       .permitAll(); 

     http 
       .authorizeRequests() 
       .antMatchers("/api/**") 
       .authenticated() 
       .and() 
       .httpBasic(); 

     http 
       .authorizeRequests() 
       .antMatchers("/","/index") 
       .authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .loginProcessingUrl("/j_spring_security_check") 
       .defaultSuccessUrl("/monitor") 
       .failureUrl("/login?error") 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .permitAll() 
       .and() 
       .logout() 
       .logoutUrl("/j_spring_security_logout") 
       .logoutSuccessUrl("/login?logout") 
       .permitAll() 
       .and() 
       .csrf() 
       .disable(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.jdbcAuthentication().dataSource(dataSource) 
       .passwordEncoder(passwordEncoder()) 
       .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?") 
       .authoritiesByUsernameQuery("SELECT username, authority FROM authorities WHERE username=?"); 
    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     PasswordEncoder encoder = new BCryptPasswordEncoder(); 
     return encoder; 
    } 
} 

.

+0

Cevabınızı kabul ettiğiniz için teşekkür ederiz. Sakıncası yoksa bana oy verebilirsiniz! – SyntaX

+1

Elbette. Yardım ettiğin için teşekkür ederim. –

cevap

13

Aşağıdaki gibi birden çok http yapılandırmasını kullanarak bunu kolayca yapabilirsiniz, bu kod yalnızca birden çok http yapılandırmasını açıklar. Sana güvenliği örn authenticationManger vs. bahar ile ilgili diğer gerekli yapılandırmaları farkında olduklarını varsayıyorum

@EnableWebSecurity 
    public class MultiHttpSecurityCustomConfig { 
     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password") 
        .roles("USER", "ADMIN"); 
     } 

     @Configuration 
     @Order(1) 
     public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
      protected void configure(HttpSecurity http) throws Exception { 
       http.antMatcher("/api/**").authorizeRequests().anyRequest().hasRole("ADMIN").and().httpBasic(); 
      } 
     } 

     @Configuration 
     public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

      @Override 
      protected void configure(HttpSecurity http) throws Exception { 
       http.authorizeRequests().anyRequest().authenticated().and().formLogin(); 
      } 


    } 
} 

bahar güvenlik yetkilisi linki bakınız: Multiple HttpSecurity

Ben de Secure REST Services with Spring Security

kontrol etmek tavsiye eder

Herhangi bir sorunla karşılaşırsanız yorum yapmaktan çekinmeyin!

İlgili konular