복붙노트

[SPRING] 스프링 보안이 작동하지 않는 이유는 무엇입니까?

SPRING

스프링 보안이 작동하지 않는 이유는 무엇입니까?

내 프로젝트에 Spring Security를 ​​통합하려고합니다.

나는 여기에 주어진 문서를 따라왔다 : https://spring.io/guides/gs/securing-web/

스프링 부트 대신 XML을 사용하여 모든 것을 구성했습니다.

내 web.xml은 다음과 같습니다.

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml :

<context:component-scan base-package="com.name.ot" />

<bean id="resolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
</bean>

그리고 내보기 컨트롤러입니다 :

@Controller
public class HomeController{

    @RequestMapping(value={"/", "/home"}, method = RequestMethod.GET)
    public ModelAndView home() {

        ModelAndView model = new ModelAndView("home");
        return model;
    }

    @RequestMapping(value={"/hello"}, method = RequestMethod.GET)
    public ModelAndView hello() {

        ModelAndView model = new ModelAndView("hello");
        return model;
    }

    @RequestMapping(value={"/login"}, method = RequestMethod.GET)
    public ModelAndView login() {

        ModelAndView model = new ModelAndView("login");
        return model;
    }
}

내 보안 클래스 :

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

그러나, /, / home, / hello, / login과 같은 모든 페이지에 액세스 할 수 있습니다.

나는 / 로그인하지 않고 사용자가 직접 / hello에 액세스하는 것을 원하지 않습니다.

내가 뭘 잘못하고 있죠?

해결법

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

    1.나는 같은 문제가 있었다.

    나는 같은 문제가 있었다.

    내 해결책은 Craig Walls p247의 "행동의 샘"책에서 언급 한 것과 같습니다. AbstractSecurityWebApplicationInitializer를 확장하는 빈 클래스를 만들어야합니다.

    public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer {}
    
  2. ==============================

    2.필터 체인 프록시를 등록해야합니다.

    필터 체인 프록시를 등록해야합니다.

    XML 설정은 Spring Security Reference :

    Java 구성 및 Servlet API 3+에 대한 자세한 내용은 Spring Security Reference :

  3. from https://stackoverflow.com/questions/39588934/why-is-spring-security-not-working by cc-by-sa and MIT license