Java Spring安全身份验证后获取HTTP状态404错误

Java Spring安全身份验证后获取HTTP状态404错误,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,在处理向我的web应用程序注入spring安全性的过程中,我遇到了问题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:schemaLo

在处理向我的web应用程序注入spring安全性的过程中,我遇到了问题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>

编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
编码滤波器
/*
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
/*
上下文配置位置
/WEB-INF/application-context.xml,/WEB-INF/application-security.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener

application-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";
}
}
@控制器
@请求映射(value=“/main”)
公共类主控制器{
@自动连线
副外观服务副外观服务;
@自动连线
(二)行政长官,;
@RequestMapping(value=“/mainFrame”,method=RequestMethod.GET)
公共字符串getMainPage(){
返回“主页”;
}
@RequestMapping(value=“/resultofsearch”,method=RequestMethod.GET)
公共字符串getSearchResult(模型模型,@ModelAttribute(“searchChar”)字符串searchResult){
List deputesAppeals=deputesAppealsService.abstractSearch(搜索结果);
model.addAttribute(“ListOfAppeals”,deputesAppeals);
返回“searchingResultPage”;
}
@RequestMapping(value=“/new”,method=RequestMethod.GET)
公共字符串getAddNewAppalPage(){
返回“addPage”;
}
@RequestMapping(value=“/new”,method=RequestMethod.POST)
公共字符串addNewAppeal(@ModelAttribute(“上诉”)DeputesAppeal DeputesAppeal){
deputesAppealService.add(deputesAppeal);
返回“主页”;
}
@RequestMapping(value=“/deleted”,method=RequestMethod.GET)
公共字符串deleteAppeal(@RequestParam(value=“id”,required=true)长id,Model){
DEPUTESAPEALSERVICE.delete(id);
model.addAttribute(“id”,id);
返回“deletedPage”;
}
@RequestMapping(value=“/editPage”,method=RequestMethod.GET)
公共字符串GetEdit(@RequestParam(value=“id”,required=true)长id,模型{
addAttribute(“editedAppeal”,deputesAppealService.getById(id));
返回“编辑页面”;
}
@RequestMapping(value=“/editPage”,method=RequestMethod.POST)
公共字符串editCurrentAppeal(@ModelAttribute(“userAttribute”)DeputesAppeal DeputesAppeal,
@RequestParam(value=“id”,required=true)整数id,模型(Model){
deputesAppeal.setNumber(id);
deputesAppealService.edit(deputesAppeal);
model.addAttribute(“id”,id);
返回“editedPage”;
}
}

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

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

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
您还需要更改登录URL

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

并在表单操作中使用此选项:

<form action="${loginUrl}" method="post">


a在my web.xml中有它。您还需要更改发出POST请求的登录URL,如下所示:并在表单中使用此URL操作:谢谢,但表单已自动配置。我可以在哪里进行此更改?请查看此帖子,了解如何在login.jsp中添加这些行。非常感谢。我的问题在下一行springSecurityFilterChain/*我将模式更改为clear“/”并且一切正常