我正在使用Spring 4.0.2.RELEASE和Spring Security 3.2.3.RELEASE使用完全java配置创建一个spring安全应用程序,没有xml。安全配置似乎正常工作,正在生成登录页面并进行身份验证。但是我的所有页面都出现了404错误。
我为每个页面设置了控制器和jsp页面。当我运行应用程序时,我看到显示控制器已映射的日志消息
Mapped "{[/ || /welcome] ... onto ... WelcomeController.welcome()
然而,当我尝试点击其中一个URL时,我会获得登录页面,然后在sucessfull登录时获得404,我在日志中看不到任何内容。
下面你会找到我的控制器,我的2个配置类和我的2个初始化。
WelcomeController.java
@Controller
public class WelcomeController {
@RequestMapping(value = {"/", "/welcome"})
public ModelAndView welcome() {
System.out.println("welcome invoked");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("welcome");
return modelAndView;
}
}
下面您将找到我的配置文件
WebConfig.java
@EnableWebMvc
@Configuration
@ComponentScan({ "com.myproject.pagegen.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver
= new InternalResourceViewResolver();
resolver .setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").hasRole("USER")
.antMatchers("/welcome").hasRole("USER")
.anyRequest().anonymous()
.and().formLogin();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
WebAppInitializer.java
public class WebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { WebConfig.class, SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
SecurityWebAppInitializer.java
public class SecurityWebAppInitializer
extends AbstractSecurityWebApplicationInitializer { }
更新
我找到了一些有趣的东西。如果我将servletMapping更改为/*
而不是/
,那么我会得到一条日志消息,显示控制器已被调用,但它没有jsp的映射。似乎它试图将jsp url映射到控制器。
welcome invoked
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/ROOT/WEB-INF/jsp/welcome.jsp] in DispatcherServlet with name 'dispatcher'
答案 0 :(得分:1)
使用Eclipse和Tomcat运行项目时遇到了类似的问题。尝试使用Spring Tool Suite运行应用程序并使用VMware vFabric tc服务器。
我还通过手动更新安装的Tomcat版本在Eclipse和Tomcat中使用它。试用最新版本7.0.54 https://tomcat.apache.org/download-70.cgi
答案 1 :(得分:0)
在我看来,您的身份验证机制中存在所有问题,而不是URL-s的控制器映射。
这是一篇关于Security Java config LINK
的好教程当您尝试在没有身份验证的情况下转到您的网页时,您将被重定向到“登录页面”。如果我理解正确 - 您的身份验证过程始终失败。在您的配置中,您无法访问&#39; / welcome&#39;和&#39; /&#39;页面未经授权。
尝试添加和更改下一步:
protected void configure(HttpSecurity http) throws Exception {
// Here you can define your custom redirections
.loginPage("[some url]")
.failureUrl("[some url]")
.loginProcessingUrl("[some url]")
.defaultSuccessUrl("[some url]")
// Here the credentials, sended to the authentification mechanizm (If you use clasic 'UsernamePasswordAuthenticationFilter')
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
// Permitions for access pages
http.authorizeRequests()
.antMatchers("/welcome", "/").permitAll()
// Access restriction without using roles
.antMatchers([some other URL-s]]).authenticated()
// or using ROLES
.antMatchers([some other URL-s]]).hasRole("USER")
// Here you can define your custom redirections for logout
.and()
.logout()
.logoutUrl("/logOut")
.logoutSuccessUrl("/welcome");
}
如果您使用clasic&#39; UsernamePasswordAuthenticationFilter&#39;你必须将它定义为bean(如果你使用另一个fllter,当然你必须定义它):
@Bean
public UsernamePasswordAuthenticationFilter filter() {
UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
filter.setAuthenticationManager(providerManager());
return filter;
}
然后在JSP页面中使用这样的表格(&#39; j_username&#39;和&#39; j_password&#39;默认身份验证中所需的参数&#39; UsernamePasswordAuthenticationFilter&#39;)
<form name="signIn" action="[your SignIn URL]" method="POST">
<input type="text" name="j_username"></label>
<input type="password" name="j_password"></label>
<input type="submit" value="Log In">
</form>
您必须尝试远程调试才能找到身份验证过程中的失败。