2014-11-09 116 views
0

我在创建我的spring 4应用程序时遇到了这个持久性错误。这里是我的代码:j_security_check中的http 404错误

Initializer.java:

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     // TODO Auto-generated method stub 
     return new Class[] { RootConfig.class, SecurityConfig.class }; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     // TODO Auto-generated method stub 
     return new Class[] { WebAppConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     // TODO Auto-generated method stub 
     return new String[] { "/" }; 
    } 

    // I have tried adding the following but it doesn't seem to work also: 
    @Override 
    protected Filter[] getServletFilters() { 
     // TODO Auto-generated method stub 
     return new Filter[] { new DelegatingFilterProxy("springSecurityFilterChain") }; 
    } 
} 

WebAppConfig.java:

@Configuration 
@EnableWebMvc 
@ComponentScan("com.myco.controller") 
public class WebAppConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/pages/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 

} 

RootConfig.java:

@Configuration 
@EnableTransactionManagement 
@ComponentScan("com.mycompany") 
@PropertySource("classpath:application.properties") 
public class RootConfig { 
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 
private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 

private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan"; 

@Resource 
private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 

     dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 
     dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 
     dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 
     dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 

     return dataSource; 
    } 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
     sessionFactoryBean.setDataSource(dataSource()); 
     sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 
     sessionFactoryBean.setHibernateProperties(hibProperties()); 
     return sessionFactoryBean; 
} 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 
     properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 
     return properties; 
} 

    @Bean 
    public HibernateTransactionManager transactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(sessionFactory().getObject()); 
     return transactionManager; 
    } 

} 

SecurityConfig.java:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private DataSource dataSource; 

    @Autowired 
    private UserDetailsService userDetailsService; 



    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http.csrf().disable().userDetailsService(userDetailsService) 
      .authorizeRequests() 
      .antMatchers("/sec/moderation.html").hasRole("MODERATOR") 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .and() 
      .formLogin() 
      .loginPage("/user-login.html") 
      .defaultSuccessUrl("/success-login.html") 
      .failureUrl("/error-login.html") 
      .permitAll() 
      .and() 
      .logout() 
      .logoutSuccessUrl("/index.html"); 
    } 

} 

最后,我登录JSP(登录-form.jsp):

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Login page</title> 
<style> 
.error { 
    color: red; 
} 
</style> 
</head> 
<body> 
<h1>Login page</h1> 

<p> 
<c:if test="${error == true}"> 
    <b class="error">Invalid login or password.</b> 
</c:if> 
</p> 

<form method="post" action="<c:url value='j_spring_security_check'/>" > 
<table> 
<tbody> 
<tr> 
<td>Login:</td> 
<td><input type="text" name="j_username" id="j_username"size="30" maxlength="40" /></td> 
</tr> 
<tr> 
<td>Password:</td> 
<td><input type="password" name="j_password" id="j_password" size="30" maxlength="32" /></td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" value="Login" /></td> 
</tr> 
</tbody> 
</table> 
</form> 

<p> 
<a href="${pageContext.request.contextPath}/index.html">Home page</a><br/> 
</p> 
</body> 
</html> 

我的错误:

enter image description here

+0

什么是春季安全的版本? – 2014-11-09 05:07:35

+0

请阅读,我使用Spring 4,上面已经说过。 – 2014-11-09 05:53:48

+0

我在问[Spring Security](http://projects.spring.io/spring-security/)它的版本与core spring不同。 – 2014-11-09 05:56:05

回答

0

如果你正在使用Spring Security 3.2,登录URL从更改为也会更改用户名和密码的参数。

用户名:从j_usernameusername

密码:从j_passwordpassword

所以你的形式会。

<form method="post" action="<c:url value='login'/>" > 
    <table> 
     <tbody> 
      <tr> 
       <td>Login:</td> 
       <td><input type="text" name="username" id="username"size="30" maxlength="40" /></td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type="password" name="password" id="password" size="30" maxlength="32" /></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td><input type="submit" value="Login" /></td> 
      </tr> 
     </tbody> 
    </table> 
</form>