java - Shiro 和 Tomcat AccessLogValve

标签 java jakarta-ee tomcat shiro

我正在使用 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/

相关文章:

java - 如何系统测试 Java 桌面应用程序?

java - 在记分板代码中使用什么单链表或数组?

java - 构建 Jar 时将外部文件夹复制到目标(使用 Maven)

java - 存储在 getPoint() 对象中的坐标

java - Spring 应用的 Permgen 空间错误

angularjs - [Angular, Jersey]为什么我必须使用 jersey 在 $http 中指定应用程序名称

jakarta-ee - Java EE Servlet 作为 XMPP 连接器的容器

java - 如何处理 DAO 中的连接对象 (Java EE)

java - 在大型应用程序中处理事务管理

java - 如何为不同的环境读取不同的属性文件?