-1

안녕하세요!스프링 보안 - Java 구성

XML 기반 프로젝트에서 프로젝트를 주석 기반/Java 구성으로 변환하려고합니다. 아래 XML 구성을 Java 구성으로 전환 할 수 있습니까?

<beans:bean id="jwtAuthenticationFilter" class="foo.bar.security.JwtAuthenticationFilter"> 
     <beans:property name="authenticationManager" ref="authenticationManager"/> 
     <beans:property name="authenticationSuccessHandler" ref="jwtAuthenticationSuccessHandler" /> 
    </beans:bean> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider ref="jwtAuthenticationProvider" /> 
    </authentication-manager> 

이것은 내가 사용하는 security-context.xml의 스 니펫입니다. here 솔루션을 찾으려고하지만 @Bean에 대한 설명서에는 없습니다. 나는 콩의 속성으로 무엇을해야할지 모른다. 또한 authentication-manager 노드의 경우 누군가 나를 도울 수 있기를 바랍니다.

미리 감사드립니다.

답변

0

필터 클래스를 선언해야합니다. 예 :

public class JwtAuthenticationFilter extends OncePerRequestFilter { 

    private final AuthenticationManager authenticationManager; 

    public JwtAuthenticationFilter(AuthenticationManager authenticationManager) { 
    this.authenticationManager = authenticationManager; 
    } 

    @Override 
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { 
    String authToken = request.getHeader("X-AUTH-TOKEN"); 
    if (authToken == null) { 
     chain.doFilter(request, response); 
     return; 
    } 
    Authentication authentication = authenticationManager.authenticate(new JwtAuthenticationToken(authToken)); 
    SecurityContextHolder.getContext().setAuthentication(authentication); 
    chain.doFilter(request, response); 
    } 
} 

그리고 SecurityConfiguration 클래스를 만듭니다. 예 :

@Configuration 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Value("${secret.key}") 
    private String secretKey; 

    @Autowired 
    private UserRepository userRepository; 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .authenticationEventPublisher(new NoopAuthenticationEventPublisher()) 
     .authenticationProvider(new JwtAuthenticationProvider(secretKey, userRepository)); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http 
     .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
     .and() 
     .csrf().disable() 
     .addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), AbstractPreAuthenticatedProcessingFilter.class) 
     .addFilterBefore(new BasicAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class) 
     .authorizeRequests() 
     .antMatchers("/admin/**").hasRole("ADMIN") 
     .antMatchers("/owner/**").hasAnyRole("OWNER", "ADMIN") 
     .antMatchers("/health", "invitation/accept").permitAll() 
     .antMatchers("/**").hasRole("USER"); 
    } 

}