2016-05-15 89 views
1

同時處理注入彈簧安全到我的web應用程序我得到問題HTTP狀態404錯誤。當我嘗試訪問我的網頁首先我得到登錄頁面這是自動配置,輸入登錄名和密碼後,這是正確的,我得到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

只是看你是否有springSecurityFilterChain在web.xml中定義

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

,並使用此表單中的作用:

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

a在我的web.xml中有它 –

+0

您還需要更改您正在進行POST重新登錄的登錄URL請求如下所示: 並在您的表單中使用此操作:

shankarsh15

+0

謝謝,但形式配置auto.where我可以做出這種變化? –