开发者社区> 问答> 正文

springmvc springsecurity问题?报错

在springmvc+springsecurity的项目中,若使用POST方式登录, 则出现以下问题:

“HTTP Status 405 - Request method 'GET' not supported”

其中security.xml配置为:

<http pattern="/loginPage" security="none"></http>
	<http  pattern="*.js" security="none"/>
	<http  pattern="*.css" security="none"/>
	<http  pattern="*.gif" security="none"/>
	<http  pattern="*.png" security="none"/>
	<http  pattern="*.jpg" security="none"/>

	<!-- HTTP拦截 -->
	<http auto-config="true" use-expressions="true">
		<intercept-url pattern="/**" access="hasRole('ROLE_USER')" method="POST"/>
		<!-- 表单登录验证 -->
		<form-login login-page="/loginPage" login-processing-url="/login"
			always-use-default-target="true" default-target-url="/welcome"
			authentication-failure-url="/loginPage?error=error"
			username-parameter="username" password-parameter="password" />
		<!-- 跨区域验证关闭 -->
		<csrf disabled="true" />
		<!-- 注销验证 -->
		<logout logout-url="/logout" logout-success-url="/loginPage"
			invalidate-session="true" />
		<!-- 自动记忆配置 -->
		<remember-me key="authorition" />
	</http>

spring mvc.xml为

	<mvc:annotation-driven />
	<!-- 使Spring支持自动检测组件,自动扫描controller -->
	<context:component-scan base-package="com.security.controller" />
	<!-- 静态资源访问 -->
	<mvc:resources location="/js/" mapping="/js/**" />
	<!-- 定义视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

两者均比较简单;

页面登录请求为:

 <form name="loginForm" action="<c:url value='/login' />" method="POST">  

而login是由springsecurity提供的, 默认是要POST方法才能登录。

现在看看/welcome这个请求的处理代码:

  @RequestMapping(value={"/welcome","/"},method=RequestMethod.POST)
    public String welcome(Model model){
    	System.out.println("Login success");

URL请求地址:

 

最后报错如下:

九月 28, 2017 11:26:50 上午 org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'GET' not supported
九月 28, 2017 11:26:50 上午 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
WARNING: Handler execution resulted in exception: Request method 'GET' not supported

 

请大神帮忙看看,谢谢。

 

展开
收起
爱吃鱼的程序员 2020-06-08 10:36:03 697 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    刚才仔细看了一下default-target-url的描述:

    Attribute:default-target-urlTheURLthatwillberedirectedtoaftersuccessfulauthentication,iftheuser'spreviousactioncouldnotberesumed.Thisgenerallyhappensiftheuservisitsaloginpagewithouthavingfirstrequestedasecuredoperationthattriggersauthentication.Ifunspecified,defaultstotherootoftheapplication.

    TheURLthatwillberedirectedtoaftersuccessfulauthentication

    redirected,是get方法了,没办法咯。

    我猜这样理解应该是正确的,大伙讨论讨论呗.

    不知道是不是对大伙有帮助呢? 

     

    @RequestMapping(value={"/welcome","/"},method=RequestMethod.POST)

    登陆成功的页面需要使用GET访问,你的是POST的,修改为RequestMethod.GET

    是的,我后面仔细去看了配置的说明

    楼上正解

    引用来自“简单仁”的评论

    刚才仔细看了一下default-target-url的描述:

    Attribute:default-target-urlTheURLthatwillberedirectedtoaftersuccessfulauthentication,iftheuser'spreviousactioncouldnotberesumed.Thisgenerallyhappensiftheuservisitsaloginpagewithouthavingfirstrequestedasecuredoperationthattriggersauthentication.Ifunspecified,defaultstotherootoftheapplication.

    TheURLthatwillberedirectedtoaftersuccessfulauthentication

    redirected,是get方法了,没办法咯。

    我猜这样理解应该是正确的,大伙讨论讨论呗.

    不知道是不是对大伙有帮助呢? 

     

    2020-06-08 10:36:16
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多