將我的應用程序切換到Spring security 3.2和Java Configuration之後遇到問題。最初它是用xml配置的,但是當我每次嘗試訪問localhost時都做了所有更改以使用註釋:8080/mysite/login我有這個錯誤「沒有找到具有URI的HTTP請求的映射[/ mysite/login ]分配給名爲「mvc-dispatcher」的DispatcherServlet。這是我的配置:無法使用Java Configuration和Spring Security 3.2登錄
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
AppConfig.java
@EnableWebMvc
@Configuration
@ComponentScan({ "com.mypackages.*" })
@Import({ DatabaseConfig.class, SecurityConfiguration.class, SocialConfig.class })
@PropertySource(value = {"classpath:/mysite.properties"})
public class AppConfig extends WebMvcConfigurerAdapter {
//in this class I define the viewResolver, messageSource, localeResolver, some properties, etc, but I don't think they would be necessary for my problem
}
「配置」 在SecurityConfiguration.java
方法protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login").defaultSuccessUrl("/welcome")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.deleteCookies("JSESSIONID")
.and()
.authorizeRequests()
.antMatchers("/welcome*").authenticated()
.and()
.rememberMe()
.and().headers().xssProtection().frameOptions().httpStrictTransportSecurity()
.and()
.apply(new SpringSocialConfigurer());
}
的web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.myapp.config.AppConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.config.AppConfig</param-value>
</context-param>
至於我明白,與登錄頁面當我訪問localhost:8080/mysite/login時,在「配置」方法(SecurityConfiguration.java)中顯示的登錄頁面應該足以顯示登錄頁面,我一直在閱讀一些示例,我不認爲這個錯誤與安全有關配置,我錯過了什麼?
任何幫助將不勝感激
UPDATE
我一直在尋找,它看起來像我的錯誤是包括web.xml和的AbstractAnnotationConfigDispatcherServletInitializer
的延伸,所以我決定刪除web.xml中,並修改SpringMvcInitializer
:
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class, SecurityConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DatabaseConfig.class };
}
但仍然有同樣的錯誤No mapping found for HTTP request with URI [/siteserver/login] in DispatcherServlet with name 'dispatcher'
,pleeeaaaase是否有人有什麼回事線索?
並啓用@EnableWebMvcSecurity添加監聽器類? – mariubog 2015-01-21 02:54:25
以及那個contextPath/mysite呢?是否宣佈? – mariubog 2015-01-21 03:11:11
Hi @ user1289300,對於遲到的回覆感謝,並感謝您的幫助。關於你的問題,對兩者都是這樣的:我確實啓用了EnableWebMvcSecurity,並且還聲明瞭/ mysite路徑 – maxivis 2015-01-24 00:19:13