2016-09-20 295 views
4

我正在嘗試在我的項目中集成Spring Security。爲什麼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,而不需要/login

我在做什麼錯?

+0

是的,我正在使用3.0我不知道如何在我的應用程序中使用此過濾器鏈代理,你能給我一個示例代碼爲我的應用程序? –

回答

1

您必須註冊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獲得您的註冊。

+0

我試着通過擴展基類'AbstractSecurityWebApplicationInitializer'來註冊'springSecurityFilterChain',但它說'沒有名爲'springSecurityFilterChain'的bean被定義了' –

+0

是的,我也試過了,但是當我做這個文件時tomcat並沒有啓動,錯誤'無法啓動'。是的,我的配置是在com.name.ot.conf –

+0

您可以請添加日誌。它應該工作。也許這個問題給你一個提示:http://stackoverflow.com/questions/37543489/no-bean-named-springsecurityfilterchain-is-defined-error-in-spring-java-based – dur

1

我有同樣的問題。

我的解決方案與Craig Walls p247的「Spring in action」一書中提到的一樣。您需要創建一個擴展AbstractSecurityWebApplicationInitializer的空類。

public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer {} 
相關問題