2013-12-17 2 views
3

나는이 다음 봄 보안 설정 :봄 보안 형태의 인증 마이그레이션

<?xml version="1.0" encoding="UTF-8"?> 
<b:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> 

<http use-expressions="true"> 
    <intercept-url pattern="/edit/**" access="hasRole('EDITOR')" /> 
    <form-login login-page="/login" authentication-failure-url="/loginfailed" /> 
    <logout logout-success-url="/" delete-cookies="JSESSIONID" /> 
    <remember-me user-service-ref="userDetailsService"/> 
</http> 

<b:bean id="encoder" 
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" /> 

<authentication-manager> 
    <authentication-provider user-service-ref="userDetailsService"> 
     <password-encoder ref="encoder" /> 
    </authentication-provider> 
</authentication-manager> 
</b:beans> 

내가 주석 기반 설정으로 마이그레이션하기 위해 노력하고있어 : 또한

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.core.userdetails.UserDetailsService; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.security.crypto.password.PasswordEncoder; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests().antMatchers("/edit/**").hasRole("EDITOR").and() 
       .logout().logoutSuccessUrl("/").deleteCookies("JSESSIONID").and() 
       .formLogin().loginPage("/login").failureUrl("/loginfailed").and() 
       .rememberMe().userDetailsService(userDetailsService); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(encoder()); 
    } 

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

} 

내가 소셜 네트워크 로그인 기능을 가지고 있고이를 위해 Autowired RequestCache를 사용했다. 그리고이 bean은 어노테이션을 기반으로 한 설정으로는 애플리케이션 컨텍스트에 나타나지 않는다. 내가 누락 된 것?

답변

1

이 RequestCache 문제는 다음과 같은 방법으로 해결 :

@Bean 
public RequestCache requestCache() { 
    return new HttpSessionRequestCache(); 
} 

을 그리고 변화 구성 :

http 
      .requestCache().requestCache(requestCache()).and() 
      .authorizeRequests().antMatchers("/edit/**").hasRole("EDITOR").and()... 

는 또한 어노테이션 기반 설정 많은 기본값으로 마이그레이션 변화 - "j_username"을 "사용자 이름" , "j_password"를 "password"로, "j_spring_security_check"를 "login"으로, "j_spring_security_logout"을 "logout"으로, csrf 숨겨진 토큰을 양식에 입력해야합니다.