【问题标题】:Why is Spring Security not working?为什么 Spring Security 不起作用?
【发布时间】:2018-07-13 13:33:13
【问题描述】:

我正在尝试将 Spring Security 集成到我的项目中。

我已遵循此处提供的文档: https://spring.io/guides/gs/securing-web/

我使用 XML 配置了所有内容,而不是 Spring Boot。

我的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,而不进入/login

我做错了什么?

【问题讨论】:

  • 是的,我使用的是 3.0 我不知道如何在我的应用程序中使用这个过滤器链代理,你能给我一个我的应用程序的示例代码吗?

标签: java spring spring-mvc spring-security


【解决方案1】:

我遇到了同样的问题。

我的解决方案在 Craig Walls 的“Spring in action”一书中 p247 中提到。您需要创建一个扩展 AbstractSecurityWebApplicationInitializer 的空类。

public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer {}

【讨论】:

    【解决方案2】:

    您必须注册Filter Chain Proxy

    XML配置见Spring Security Reference:

    当使用 servlet 过滤器时,显然需要在 web.xml 中声明它们,否则它们将被 servlet 容器忽略。在 Spring Security 中,过滤器类也是在应用程序上下文中定义的 Spring bean,因此能够利用 Spring 丰富的依赖注入设施和生命周期接口。 Spring 的 DelegatingFilterProxy 提供了 web.xml 和应用程序上下文之间的链接。

    使用DelegatingFilterProxy 时,您会在web.xml 文件中看到类似这样的内容:

    <filter>
        <filter-name>myFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>myFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    有关 Java 配置和 Servlet API 3+,请参阅Spring Security Reference

    下一步是在战争中注册springSecurityFilterChain。这可以在 Servlet 3.0+ 环境中使用 Spring 的 WebApplicationInitializer 支持在 Java 配置中完成。毫不奇怪,Spring Security 提供了一个基类AbstractSecurityWebApplicationInitializer,它将确保为您注册springSecurityFilterChain

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-22
      • 2017-05-13
      • 2016-12-15
      • 2014-01-28
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多