我正在使用 Shiro
(v1.2.1) 作为安全框架,它工作得很好,但是在 Tomcat
(v7.0.32) 的访问日志中你无法远程访问当经过身份验证的用户访问任何资源时,用户登录。对于我的示例 Web 应用程序,我使用了 Shiro
SVN 示例中提供的默认配置 repo .
127.0.0.1 - - [13/Nov/2012:08:22:55 +0200] "POST /pacs/login.jsp HTTP/1.1" 302 - // User here not logged
127.0.0.1 - - [13/Nov/2012:08:22:55 +0200] "GET /pacs/ HTTP/1.1" 200 821 // Here user is accessing protected page, so we got permission.
有什么办法可以解决这个问题吗?
最佳答案
我不确定这是否是官方解决方案,但它是适合我的解决方案:
对于 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.
在我的例子中,我只是修改了登录代码以将用户对象写入 session 。 Tomcat 将在此对象上调用 .toString() 以打印“猜测的用户名”。
如果 .toString() 的结果不是你想要的,你也可以尝试使用
%{xxx}s xxx is an attribute in the HttpSession
在您的 AccessLogValve 配置中,以便从您的 session 中打印另一个属性。
希望对您有所帮助。
关于java - Shiro 和 Tomcat AccessLogValve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356404/