Java Spring安全问题-404错误
我只是试图执行一个非常简单的Spring安全示例项目,但我得到了404错误。请在此处帮助查找问题 控制器:Java Spring安全问题-404错误,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我只是试图执行一个非常简单的Spring安全示例项目,但我得到了404错误。请在此处帮助查找问题 控制器: package mypack; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; imp
package mypack;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SecurityController {
@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is welcome page!");
model.setViewName("hello");
return model;
}
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page!");
model.setViewName("admin");
return model;
}
}
spring-servlet.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="mypack" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
/WEB-INF/views/
.jsp
spring-security.xml
<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.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<http auto-config="true">
<intercept-url pattern="/admin**" access="ROLE_USER" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
hello.jsp
<%@page session="false"%>
<html>
<body>
<h1>Title : ${title}</h1>
<h1>Message : ${message}</h1>
标题:${Title}
消息:${Message}
在您的spring security xml文件中尝试以下代码。还根据您的要求更改了角色
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/signin" access="permitAll"></intercept-url>
<intercept-url pattern="/logout" access="permitAll"></intercept-url>
<intercept-url pattern="/accessdenied" access="permitAll"></intercept-url>
<intercept-url method="GET" pattern="/**"
access="hasRole('USER')
or hasRole('ADMIN')"></intercept-url>
<form-login login-page="/signin" default-target-url="/index"
authentication-failure-url="/accessdenied" always-use-default-target="true"
username-parameter="username" password-parameter="password"></form-login>
<logout logout-success-url="/logout"></logout>
</http>
尝试spring security xml文件中的以下代码。还可以根据您的要求更改角色
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/signin" access="permitAll"></intercept-url>
<intercept-url pattern="/logout" access="permitAll"></intercept-url>
<intercept-url pattern="/accessdenied" access="permitAll"></intercept-url>
<intercept-url method="GET" pattern="/**"
access="hasRole('USER')
or hasRole('ADMIN')"></intercept-url>
<form-login login-page="/signin" default-target-url="/index"
authentication-failure-url="/accessdenied" always-use-default-target="true"
username-parameter="username" password-parameter="password"></form-login>
<logout logout-success-url="/logout"></logout>
</http>
因为您已经在web.xml到url模式中将servlet映射配置为servlet映射中的“/”。因此,DispatcherServlet将查找名为index.html/jsp的文件 要配置控制器,必须将组件扫描添加到servlet.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="mypack" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
添加下面的行
<context:component-scan base-package="mypack" />
因为您已经在web.xml到url模式中将servlet映射配置为servlet映射中的“/”。因此,DispatcherServlet将查找名为index.html/jsp的文件 要配置控制器,必须将组件扫描添加到servlet.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="mypack" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
添加下面的行
<context:component-scan base-package="mypack" />
您必须定义路径的过滤器:
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/welcome" access="permitAll" />
因此,在这种情况下,没有角色(匿名)的用户可以访问“/”和“/欢迎**”。您必须定义路径的筛选器:
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/welcome" access="permitAll" />
因此,在这种情况下,没有角色(匿名)的用户可以访问“/”和“/欢迎**”。组件扫描扫描控制器的注释组件将解决spring-servlet.xml中存在的问题。很抱歉,它在代码段中不可见。现在编辑它。我开始得到404错误,一旦我包括了spring安全配置。若我对web.xml中的spring安全配置进行注释,那个么该项目运行良好。请注意,我没有包括登录页面。我希望spring.component scan提供的默认登录页面能够扫描控制器的注释组件,以解决spring-servlet.xml中存在的问题。很抱歉,它在代码段中不可见。现在编辑它。我开始得到404错误,一旦我包括了spring安全配置。若我对web.xml中的spring安全配置进行注释,那个么该项目运行良好。请注意,我没有包括登录页面。我期待spring提供的默认登录页面。存在于spring-servlet.xml中。很抱歉,它在代码段中不可见。现在编辑它。存在于spring-servlet.xml中。很抱歉,它在代码段中不可见。现在编辑它。