dimanche 8 mars 2015

spring security custom login page return 404

I followed this to set up my custom login page with spring security.The problem is when access /, it will redirect to /account/login, and this /account/login will return 404 Error: NOT_FOUND. How to fix this issue.


spring-security.xml file



<beans:beans xmlns="http://ift.tt/1c8inpe"
xmlns:beans="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/1c8inpe
http://ift.tt/1epvZ6L">

<http auto-config="true">
<intercept-url pattern="/" access="ROLE_USER" />
<form-login
login-page="/account/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="email"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>

<beans:bean name="customUserDetailsService" class="com.example.web.CustomUserDetailsService">
</beans:bean>

<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService">
</authentication-provider>
</authentication-manager>
</beans:beans>


spring-servlet.xml file



<beans xmlns="http://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/GArMu7
http://ift.tt/QEDs1k
http://ift.tt/1bHqwjR
http://ift.tt/1bVJL9q">

<context:component-scan base-package="com.example.web.*" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


CustomUserDetailsService file



public class CustomUserDetailsService implements UserDetailsService {

@Override
public UserDetails loadUserByUsername(String email)
throws UsernameNotFoundException {

if (email == null || email.trim().equals("")) {
throw new UsernameNotFoundException("Invalid email address.");
}

try (Connection conn = Config.getConn()) {

String query = "xxx";

PreparedStatement stm = conn.prepareStatement(query);

stm.setString(1, email);

ResultSet rs = stm.executeQuery();

if (!rs.next()) {
throw new UsernameNotFoundException(
"User does not exist.");
}

if (rs.getBoolean("delete")) {
throw new UsernameNotFoundException("User has been deleted.");
}

Collection<? extends GrantedAuthority> authorities = AuthorityUtils
.createAuthorityList("USER");

String password = rs.getString("pass");

UserDetails user = new User(email, password, true, true, true,
true, authorities);

return user;

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

throw new UsernameNotFoundException("Unable to connect to database.");
}
}


AccountController file



@Controller
@RequestMapping("/account")
public class AccountController {

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String Index(Model model) {
return "login";
}
}


HomeController file



@Controller
@RequestMapping("/")
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String Index(Model model) {
return "index";
}
}


web.xml file



<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app xmlns="http://ift.tt/nSRXKP" xmlns:web="http://ift.tt/LU8AHS" xmlns:xsi="http://ift.tt/ra1lAU" version="2.5" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/LU8AHS">

<servlet>
<servlet-name>myweb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myweb</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.example.app.xxx.yyyService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>

<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


log from google app engine



ip.ip.ip.ip - - [05/Mar/2015:19:41:49 -0800] "GET /account/login HTTP/1.1" 404 204 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36" "xxx.appspot.com" ms=830 cpu_ms=606 cpm_usd=0.000023 instance=00c61bxxxxxxxxxxxxxxxxxx624e0fc app_engine_release=1.9.18 trace_id=60de930xxxxxxxxxxxxxxxxxbb7ab5



EDIT


The problem is inside servlet.xml, change <context:component-scan base-package="com.example.web.*" /> to <context:component-scan base-package="com.example.web" /> and it will work.


Aucun commentaire:

Enregistrer un commentaire