Spring Security身份验证后获取HTTP状态404错误

时间:2016-05-15 09:54:36

标签: java spring spring-mvc spring-security

在处理弹簧安全注入到我的Web应用程序时,我遇到问题HTTP状态404错误。当我尝试访问我的页面时,首先我得到登录页面是自动配置,在输入登录名和密码后,这是正确的,我得到HTTP状态404错误。代码

的web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml, /WEB-INF/application-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>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

应用的security.xml

<?xml version="1.0" encoding="UTF-8"?>
<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-4.1.xsd
                http://www.springframework.org/schema/security
                http://www.springframework.org/schema/security/spring-security-4.1.xsd">

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/main/**" access="hasRole('ROLE_USER')" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="admin" password="adminpassword" authorities="ROLE_USER, ROLE_ADMIN" />
            <user name="user" password="userpassword" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

控制器

@Controller
@RequestMapping(value = "/main")
public class MainController {

@Autowired
DeputesAppealService deputesAppealService;

@Autowired
DeputesAppealDao deputesAppealDao;


@RequestMapping(value = "/mainFrame", method = RequestMethod.GET)
public String getMainPage(){
    return "mainPage";
}

@RequestMapping(value = "/resultOfSearching", method = RequestMethod.GET)
public String getSearchResult(Model model, @ModelAttribute("searchChar")String searchResult)  {
    List<DeputesAppeal> deputesAppeals = deputesAppealService.abstractSearch(searchResult);
    model.addAttribute("ListOfAppeals", deputesAppeals);
    return "searchingResultPage";
}

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String getAddNewAppealPage(){
    return "addPage";
}


@RequestMapping(value = "/new", method = RequestMethod.POST)
public String addNewAppeal(@ModelAttribute("Appeal")DeputesAppeal deputesAppeal) {
    deputesAppealService.add(deputesAppeal);
    return "mainPage";
}


@RequestMapping(value = "/deleted", method = RequestMethod.GET)
public String deleteAppeal(@RequestParam(value = "id", required = true) Long id, Model model){
    deputesAppealService.delete(id);
    model.addAttribute("id", id);
    return "deletedPage";
}

@RequestMapping(value = "/editPage", method = RequestMethod.GET)
public String GetEdit(@RequestParam(value = "id", required = true) Long id, Model model){
    model.addAttribute("editedAppeal", deputesAppealService.getById(id));
    return "editPage";
}

@RequestMapping(value = "/editPage", method = RequestMethod.POST)
public String editCurrentAppeal(@ModelAttribute("userAttribute") DeputesAppeal deputesAppeal,
                                @RequestParam(value = "id", required = true)Integer id, Model model) {
    deputesAppeal.setNumber(id);
    deputesAppealService.edit(deputesAppeal);
    model.addAttribute("id", id);
    return "editedPage";
}
}

1 个答案:

答案 0 :(得分:1)

看看你的web.xml中是否定义了 springSecurityFilterChain

com.sun.net.httpserver

您还需要更改登录网址

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

并在表单操作中使用它:

<c:url value="/j_spring_security_check" var="loginUrl" />