【问题标题】:No mapping found for HTTP request with URI j_spring_security_check in DispatcherServlet在 DispatcherServlet 中找不到带有 URI j_spring_security_check 的 HTTP 请求的映射
【发布时间】:2016-02-28 00:18:36
【问题描述】:

这是我的 Spring Security Bean

   <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.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http auto-config="true">
        <intercept-url pattern="/welcome*" access="ROLE_USER" />
        <form-login login-page="/login" default-target-url="/welcome"
            authentication-failure-url="/loginfailed" />
        <logout logout-success-url="/logout" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="username" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

这是我的 MVC-dispatcher servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />
    <tx:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.apsas" />



    <!-- Required for body marshalling -->

    <mvc:resources mapping="resources/**" location="/WEB-INF/resources/" />

    <bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="apsaspu" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>



    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />



    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/mvc-dispatcher-servlet.xml,
        /WEB-INF/spring-security.xml</param-value>
    </context-param>

    <!-- Spring Security -->
    <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>

</web-app>

这是我的控制器

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


    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String printWelcome(ModelMap model, Principal principal) {

        String name = principal.getName();
        model.addAttribute("username", name);
        model.addAttribute("message", "Spring Security Custom Form example");
        return "hello";

    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(ModelMap model) {

        return "login";

    }

    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
    public String loginerror(ModelMap model) {

        model.addAttribute("error", "true");
        return "login";

    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {

        return "login";

    }
    }

我的问题是当我访问 localhost:8080/apsas/c/welcome 我得到了

HTTP ERROR 500

 Server Error
java.lang.NullPointerException
    at com.apsas.controller.MainController.printWelcome(MainController.java:30)

我可以成功进入登录页面,但在单击提交按钮后,我得到 http 错误 404 访问 /apsas/c/j_spring_security_check 时出现问题。

这是日志 警告:在名为 'mvc-dispatcher' 的 DispatcherServlet 中找不到带有 URI [/apsas/c/j_spring_security_check] 的 HTTP 请求的映射

我可以成功访问我的磁贴中定义的某些页面,除了应该引导我登录的欢迎页面。请帮助

【问题讨论】:

  • 无论如何我都在使用 apache 瓦片
  • 你能显示你的登录页面吗?
  • 其实,没关系,我看到了问题。
  • 我可以知道解决方案吗?请帮忙。
  • 哈,我想我好像刚刚起飞。我把答案放在下面。

标签: java spring security spring-security


【解决方案1】:

spring security默认登录页面为[application context]/j_spring_security_check

您的登录页面正在提交给/apsas/c/j_spring_security_check。假设您的应用程序上下文是apsas,您的登录表单应该提交到/apsas/j_spring_security_check../j_spring_security_check

明确地说,这是登录页面上form 元素的action 属性的值。如果您需要帮助,请将您的登录页面发布到问题中,我可以更具体。

【讨论】:

  • 我的表单值是“j_spring_security_check”我尝试将其更改为“../j_spring_security_check”谢谢,将在一秒钟内回复
  • 当我注销时,我可以知道如何重定向某些页面以登录页面...无论如何它都有效
  • 当注销然后试图去localhost:8080/apsas/c/addStudentPage我重定向到localhost:8080/apsas/login我可以知道如何将它重定向到localhost:8080/apsas/c/login
  • 您可以通过安全配置 xml 中 &lt;logout&gt; 元素的 logout-success-url 指定注销页面
  • 现在解决了。刚刚更改了 呵呵,非常感谢 Taylor 先生。我非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2017-09-17
  • 2016-06-11
  • 2021-08-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 2015-06-03
相关资源
最近更新 更多