【问题标题】:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists - spring security源服务器没有找到目标资源的当前表示或不愿意透露存在的表示 - spring security
【发布时间】:2017-10-21 11:27:51
【问题描述】:

我已使用 spring security 对我的应用程序进行身份验证。配置应用程序后,我什至无法查看登录页面。我为登录页面提供了匿名访问,因此可以直接访问它。但是当我访问登录页面时,它返回 404 错误,因此我无法使用该应用程序。我使用了 JDBC 身份验证,并且通过导入另一个 xml 来获取所需的 bean。

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "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.xsd">

    <bean id = "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
      <property name = "url" value = "jdbc:mysql://localhost:3306/Employee_Management"/>
      <property name = "username" value = "root"/>
      <property name = "password" value = "root"/>
   </bean>

   <bean id = "transactionManager" 
      class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name = "dataSource"  ref = "dataSource" />
   </bean>

   <bean id = "employeeJDBCTemplate" class = "com.utility.EmployeeJDBCTemplate">
     <property name = "dataSource"  ref = "dataSource" />
     <property name = "transactionManager" ref = "transactionManager"/>
   </bean>

</beans>

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

    <beans:import resource="Beans.xml"/>

    <http entry-point-ref="loginUrlAuthenticationEntryPoint" auto-config="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/j_spring_security_check" access="isAnonymous()"/>
        <intercept-url pattern="/login" access="isAnonymous()"/>
        <intercept-url pattern="/welcome" access="hasRole('ROLE_ADMIN')"/>
        <form-login login-processing-url="/j_spring_security_check" login-page="/login" default-target-url="/welcome"
            authentication-failure-url="/login?error" username-parameter="username"
            password-parameter="password" />
        <logout logout-url="/j_spring_security_logout" invalidate-session="true" logout-success-url="/login?logout"/>
    </http>

    <beans:bean id="employeeAuthenticationProvider" class="com.authentication.EmployeeAuthenticationProvider">
      <beans:property name="employeeJDBCTemplate" ref="employeeJDBCTemplate" />
    </beans:bean>

    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="employeeAuthenticationProvider"/>
    </authentication-manager>

    <beans:bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:constructor-arg value="/login"/>
    </beans:bean>
</beans:beans>

EmployeeManagement.xml

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

   <mvc:annotation-driven />

   <mvc:default-servlet-handler/>

   <context:component-scan base-package = "com.controller" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

EmployeeController.java

package com.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

public class EmployeeController {

    @RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
    public ModelAndView welcome() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "This is welcome page");
        model.addObject("message", "Welcome to Employee Management");
        model.setViewName("hello");
        return model;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(
            @RequestParam(value = "error", required = false)String error,
            @RequestParam(value = "logout", required = false)String logout) {
        System.out.println("Show login page");
        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid username and password");
        }
        if (logout != null) {
            model.addObject("msg", "Logged out successfully");
        }
        model.setViewName("login");
        return model;
    }
}

【问题讨论】:

    标签: java xml spring spring-mvc spring-security


    【解决方案1】:

    在将两个控制器文件合并为一个文件时,我错过了控制器文件中的@Controller注解,导致登录页面出现404错误。

    【讨论】:

      【解决方案2】:

      尝试将配置更改为具有

      <intercept-url pattern="/login*" access="permitAll" />
      

      【讨论】:

      • 我已经尝试了所有可能的组合 - /login、/login*、/login**、permitAll、isAnonymous()。它不适用于任何形式。实际上,该应用程序以前运行良好。突然间,它的行为开始与昨天不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2018-04-07
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多