相关疑难解决方法(0)

在JSP EL表达式中获取Spring Security Principal

我正在使用Spring MVC和Spring Security 3.0.6.RELEASE.在JSP中获取用户名的最简单方法是什么?或者甚至只是用户是否登录?我可以想到几个方面:

1.使用scriptlet

使用这样的scriptlet来确定用户是否已登录:

<%=org.springframework.security.core.context.SecurityContextHolder.getContext()
    .getAuthentication().getPrincipal().equals("anonymousUser")
    ? "false":"true"%>
Run Code Online (Sandbox Code Playgroud)

我不喜欢使用scriptlet,我想在一些<c:if>标签中使用它,这需要将其作为页面属性放回去.

2.使用SecurityContextHolder

我可以再次使用我的SecurityContextHolder @Controller并将其放在模型上.不过,我在每个页面都需要这个,所以我宁愿不必在每个控制器中添加这个逻辑.

我怀疑有一个更清洁的方法来做到这一点......

java spring jsp spring-mvc spring-security

43
推荐指数
7
解决办法
7万
查看次数

标签 统计

java ×1

jsp ×1

spring ×1

spring-mvc ×1

spring-security ×1