spring security自定义登录页面返回404

时间:2015-03-06 02:49:57

标签: spring google-app-engine spring-mvc spring-security

我按照this设置了具有spring security的自定义登录页面。问题是访问/时,它会重定向到/account/login,此/account/login会返回404 Error: NOT_FOUND。如何解决这个问题。

spring-security.xml档案

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <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档案

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <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档案

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档案

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

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

HomeController档案

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

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

web.xml档案

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <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>

从谷歌应用引擎

登录
  

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

修改

问题出在servlet.xml中,将<context:component-scan base-package="com.example.web.*" />更改为<context:component-scan base-package="com.example.web" />并且它可以正常工作。

1 个答案:

答案 0 :(得分:0)

首先,确保您可以直接访问登录页面。

在您的web.xml中,查看您为调度员使用的网址格式。这是我的:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

然后,确保您可以直接请求/login.htm。

登录后,请更改spring-security.xml以附加&#34; .htm&#34;到您的网址。所以你的片段可能如下所示:

<form-login 
            login-page="/login.htm" 
            default-target-url="/welcome.htm" 
            authentication-failure-url="/login.htm?error" 
            username-parameter="email"
            password-parameter="password" />
        <logout logout-success-url="/login.htm?logout" />
相关问题