Java Shiro和Tomcat辅助阀

Java Shiro和Tomcat辅助阀,java,jakarta-ee,tomcat,shiro,Java,Jakarta Ee,Tomcat,Shiro,我使用Shiro(v1.2.1)作为安全框架,它工作得非常好,但是在Tomcat(v7.0.32)的访问日志中,当经过身份验证的用户访问任何资源时,您无法让远程用户登录。对于示例web应用程序,我使用了ShiroSVN示例中提供的默认配置 有什么办法可以解决这个问题吗?我不确定这是否是正式的解决方案,但这是一个适合我的解决方案: 对于AccessLogValve模式,使用 %u - Remote user that was authenticated (if any), else '-' 这将

我使用
Shiro
(v1.2.1)作为安全框架,它工作得非常好,但是在
Tomcat
(v7.0.32)的访问日志中,当经过身份验证的用户访问任何资源时,您无法让远程用户登录。对于示例web应用程序,我使用了
Shiro
SVN示例中提供的默认配置


有什么办法可以解决这个问题吗?

我不确定这是否是正式的解决方案,但这是一个适合我的解决方案:

对于AccessLogValve模式,使用

%u - Remote user that was authenticated (if any), else '-'
这将打印“猜测的用户名”。如果你问google tomcat如何猜出用户名,你会发现类似这样的情况:

For the user name, the manager uses the Session.getPrincipal() if available; 
if not, it tries the following Session attribute names:
  Login
  User
  userName
  UserName
  Utilisateur
using the given case, lower case, and upper case.  Failing that, it searches for
attributes that are instances of java.security.Principal or
javax.security.auth.Subject. 

在我的例子中,我只是修改了登录代码,将用户对象写入会话。Tomcat将在此对象上调用.toString(),以便打印“猜测的用户名”

如果.toString()的结果不是您想要的,您也可以尝试使用

%{xxx}s xxx is an attribute in the HttpSession
在AccessLogValve配置中,以打印会话中的另一个属性


希望有帮助。

如果您使用Shiro的本机会话管理,session.getPrincipal()将按预期工作。如果您希望在默认会话管理中看到此呼叫工作,请打开Jira问题,我们将查看是否可以尽快解决。

如果Shiro能够与其他工具集成,这将是一件好事,我很快就会处理问题,谢谢你的回复。我正在使用org.apache.shiro.web.session.mgt.DefaultWebSessionManager,在我的访问日志中没有任何用户名。你能给我指出正确的方向吗?“修改了登录代码以将用户对象写入会话。”如果你能解释一下这一点会很有帮助,因为我不知道你在说什么登录代码,也不知道如何将用户对象写入会话。@JimFord:“登录代码”是指验证用户的代码。某种类型的loginController,或者可能只是成功登录后被击中的第一个控制器-您已经拥有登录用户,然后可以将其写入会话。
%{xxx}s xxx is an attribute in the HttpSession