Jer*_*Orr 43 java spring jsp spring-mvc spring-security
我正在使用Spring MVC和Spring Security 3.0.6.RELEASE.在JSP中获取用户名的最简单方法是什么?或者甚至只是用户是否登录?我可以想到几个方面:
使用这样的scriptlet来确定用户是否已登录:
<%=org.springframework.security.core.context.SecurityContextHolder.getContext()
.getAuthentication().getPrincipal().equals("anonymousUser")
? "false":"true"%>
Run Code Online (Sandbox Code Playgroud)
我不喜欢使用scriptlet,我想在一些<c:if>
标签中使用它,这需要将其作为页面属性放回去.
我可以再次使用我的SecurityContextHolder @Controller
并将其放在模型上.不过,我在每个页面都需要这个,所以我宁愿不必在每个控制器中添加这个逻辑.
我怀疑有一个更清洁的方法来做到这一点......
ale*_*phx 56
检查Spring安全标签: <sec:authentication property="principal.username" />
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html
你可以检查是否记录:
<sec:authorize access="isAuthenticated()">
Run Code Online (Sandbox Code Playgroud)
而不是c:if
Sam*_*pta 17
我知道该帖子中还有其他答案,但没有人回答你如何检查用户是否经过身份验证.所以我正在分享我的代码看起来像什么.
在项目中包含标记lib:
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
Run Code Online (Sandbox Code Playgroud)
然后通过添加:在当前范围内创建用户对象:
<sec:authentication var="user" property="principal" />
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过添加轻松显示用户名.请记住,'principal'对象通常是string类型,除非您已经实现了spring security以将其更改为项目中的另一个Class:
<sec:authorize access="hasRole('ROLE_USER') and isAuthenticated()">
${user}
</sec:authorize>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助那些想要检查用户角色的人.
如果您正在使用Maven,那么请添加Christian Vielma在此主题中提到的依赖标记.
谢谢!
小智 11
您可以像这样使用:Spring Security Tag Lib - 3.1.3.RELEASE
<sec:authentication var="principal" property="principal" />
Run Code Online (Sandbox Code Playgroud)
然后:
${principal.username}
Run Code Online (Sandbox Code Playgroud)
我正在使用Maven,所以我不得不添加taglibs库,将其添加到pom.xml中
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后在我的jsp中添加:
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
Run Code Online (Sandbox Code Playgroud)
和:
<sec:authentication property="principal" />
Run Code Online (Sandbox Code Playgroud)
principal.username
一直给我错误(也许是我创建UsernamePasswordAuthenticationToken
对象的方式,不确定).
我同意alephx,我甚至投了他的答案.
但是如果你需要另一种方法,你可以使用Spring Roo使用的方法.
如果您有SecurityContextHolderAwareRequestFilter,它使用访问SecurityContext的请求包装器提供标准的servlet API安全方法.
此过滤器使用<http>
Spring Security命名空间中的标记进行注册.您也可以在FilterChainProxy的安全过滤器链中注册它(只需在applicationContext-security.xml中添加对已声明bean的引用)
然后,您可以像Roo一样访问安全servlet API(找到footer.jspx以查看如何编写条件注销链接)
<c:if test="${pageContext['request'].userPrincipal != null}">
<c:out value=" | "/>
...
Run Code Online (Sandbox Code Playgroud)
我认为<sec:authentication property="principal.username" />
并不总是有效,因为返回的类型Authentication.getPrincipal()
是Object,即:它可能是UserDetail(上面的内容将起作用),String或其他任何东西.
为了在JSP页面中显示用户名,我发现使用它更可靠${pageContext.request.userPrincipal.name}
.
这使用java.security.Principal.getName()
返回String.
这适用于用户是否登录,并且在使用匿名身份验证时有效:
<sec:authorize access="isAuthenticated()">
<sec:authentication property="principal.username" var="username" />
</sec:authorize>
<sec:authorize access="!isAuthenticated()">
<sec:authentication property="principal" var="username" />
</sec:authorize>
Run Code Online (Sandbox Code Playgroud)
后来...
Hello ${username}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
67627 次 |
最近记录: |