spring 安全模块在jsp中误用引起的问题

一. 

<%@ taglib prefix="sec"
	uri="http://www.springframework.org/security/tags"%>
<%
<input type="hidden" value="<sec:authentication property="principal.username" />">
<!--购物车表单-->

以上代码是在购物车中,用了spring的安全模块里的标签语句。

二.报错:

org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.AnonymousAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

三.分析日志报告是安全模块的principal.username不可用。

于是做如下调试:

<!--<input type="hidden" value="<sec:authentication property="principal.username" />">  -->

把该标签语句在jsp中注释掉,重启服务器,发现还是报相同的错误。

接下来,删除该input标签,重启服务器,不报错了,恢复正常。

后来分析发现,<sec:authentication property="principal.username" />只有在用户登录了(即在spring安全模块中用户已通过认证),才能用,这里要配合其他标签语句一起用,检测用户是否登录。

四.调整

<!-- 用户登录后才显示如下代码 ,开始-->
				<sec:authorize access="isAuthenticated()">
					<input type="hidden"
						value="<sec:authentication property="principal.username" />">
				</sec:authorize>
				<!-- 用户登录后才显示如下代码,结束 -->

五.总结

1.调试代码要注意jsp中的HTML注释  <!-- 注释内容 --> 在编译时可见,即里面的内容如果有错误,编译器还是会报错,所以调试时直接删除才行,不然还是会有编译错误。

<!-- this is an html comment.it will show up int the response. -->

JSP页注释(隐藏注释):指注释虽然写在JSP程序中,但不会发送给客户,因此在客户端查看源代码时不能看见注释。这样的注释在JSP编译时被忽略掉。

<%--this is a JSP comment.it will only be seen in jsp code--%>

Java注释:只能出现在Java代码区中,不允许直接出现在页面中。//单行注释   /*多行注释*/

 

转载于:https://my.oschina.net/Cubicluo/blog/830940

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值