복붙노트

[SPRING] 봄 보안 HTTP RESTFul에 기본 및 웹용 FormLogin (쿠키) - 주석

SPRING

봄 보안 HTTP RESTFul에 기본 및 웹용 FormLogin (쿠키) - 주석

구체적으로

특정 URL 패턴에 대해서만 HTTP 기본 인증을 원합니다.

상세히

내 응용 프로그램 용 API 인터페이스를 만들고 있는데 간단한 HTTP 기본 인증으로 인증해야합니다. 그러나 다른 웹 페이지는 HTTP 기본을 사용해서는 안되며 일반 양식 로그인을 사용해서는 안됩니다.

현재 구성 - 작동하지 않음

@Override
protected void configure(HttpSecurity http) throws Exception {
    http //HTTP Security
            .csrf().disable() //Disable CSRF
            .authorizeRequests() //Authorize Request Configuration
                .antMatchers("/connect/**").permitAll()
                .antMatchers("/", "/register").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/api/**").hasRole("API")
                .anyRequest().authenticated()
            .and() //HTTP basic Authentication only for API
                .antMatcher("/api/**").httpBasic()
           .and() //Login Form configuration for all others
                .formLogin().loginPage("/login").permitAll()
            .and() //Logout Form configuration
                .logout().permitAll();

}

해결법

  1. ==============================

    1.2 일을 기다렸고 여기에서 어떤 도움도 얻지 못했습니다. 그러나 나의 연구는 나에게 해결책을 제공했다 :)

    2 일을 기다렸고 여기에서 어떤 도움도 얻지 못했습니다. 그러나 나의 연구는 나에게 해결책을 제공했다 :)

    해결책

    @Configuration
    @EnableWebMvcSecurity
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Autowired
        private AuthenticationProvider authenticationProvider;
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(authenticationProvider);
        }
    
        @Configuration
        @Order(1)
        public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable()
                        .antMatcher("/api/**")
                        .authorizeRequests()
                            .anyRequest().hasAnyRole("ADMIN", "API")
                            .and()
                        .httpBasic();
            }
        }
    
        @Configuration
        @Order(2)
        public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
    
            @Override
            public void configure(WebSecurity web) throws Exception {
                web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable() //HTTP with Disable CSRF
                        .authorizeRequests() //Authorize Request Configuration
                            .antMatchers("/connect/**").permitAll()
                            .antMatchers("/", "/register").permitAll()
                            .antMatchers("/admin/**").hasRole("ADMIN")
                            .anyRequest().authenticated()
                            .and() //Login Form configuration for all others
                        .formLogin()
                            .loginPage("/login").permitAll()
                            .and() //Logout Form configuration
                        .logout().permitAll();
            }
        }
    }
    
  2. ==============================

    2.나는 도움이 될 수 있다면 몰라,하지만 위의 솔루션을 구현할 수 없습니다. 하나의 보안을 정의하는 해결 방법을 찾았습니다.

    나는 도움이 될 수 있다면 몰라,하지만 위의 솔루션을 구현할 수 없습니다. 하나의 보안을 정의하는 해결 방법을 찾았습니다.

    확장

    httpBasic () 및 formLogin ()이 모두 구성되어 있어야합니다. 그런 다음 사용자 정의를 만들었습니다.

    이 논리는 시작 방법에 있습니다.

        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException
       {
            String urlContext = UtilityClass.extractUrlContext(request);
            if (!urlContext.equals(API_URL_PREFIX))
            {
                String redirectUrl = "urlOfFormLogin"
                response.sendRedirect(request.getContextPath() + redirectUrl);
           }
            else
            {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            }
    

    이 이슈에 대한 "모범 사례 전략"인 던노

  3. from https://stackoverflow.com/questions/27774742/spring-security-http-basic-for-restful-and-formlogin-cookies-for-web-annotat by cc-by-sa and MIT license