Hibernate的春天Web应用程序如何登录用户名 [英] Hibernate envers spring web app how to log username

查看:83
本文介绍了Hibernate的春天Web应用程序如何登录用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hiberbate 3.5和spring 3.x



我有envers的工作和... _aud和revinfo记录现在正在写入。

我现在需要将用户名添加到审计记录中我猜,revinfo是我见过的seam应用程序示例的最佳位置,但对于jboss中的spring应用程序没有任何效果



任何人都可以为此提供帮助。



我的主要目标是能够记录Windows认证用户

解决方案

在Envers文档对此有一个答案,如果你使用Seam的话。

  public class ExampleListener实现RevisionListener {
public void newRevision(Object revisionEntity){
ExampleRevEntity exampleRevEntity =(ExampleRevEntity)revisionEntity;
身份标识=(身份)Component.getInstance(org.jboss.seam.security.identity);

exampleRevEntity.setUsername(identity.getUsername());




$ b所以,我想你的问题将是如何检索如果您不使用Seam,则当前登录的用户 ?我确信有几种方法可以做到,我们这样做。

.jguru.com /条/服务器端/写入servlet的filters.html相对= nofollow>写ServletFilter中
  • 在过滤器获取从主会话(或安全上下文,如果您使用像Spring Security这样的安全框架)

  • 将其存储在 log4j MDC



  • <强>代码例如由过滤器

     保护无效beforeRequest(最终HttpServletRequest的请求,最终的String消息){
    final Principal principal = request.getUserPrincipal();
    final字符串username = principal!= null? principal.getName():null;
    if(username!= null){
    MDC.put(USER,username);


    protected void afterRequest(final HttpServletRequest request,final String message){
    MDC.remove(USER);

    $ / code>

    稍后,您可以从代码中的任何位置获取用户,因为MDC具有static get(String)方法。


    I am using hiberbate 3.5 and spring 3.x

    I have envers working and the ... _aud and revinfo record are now being written.

    I now need to add the username to the audit record I'm guessing revinfo is the best place I have seen an example for seam app but nothing for spring app in jboss

    Can anyone help with this please.

    My main aim is to be able to record the windows authenticated user.

    解决方案

    The Envers documentation has an answer for this if you use Seam.

    public class ExampleListener implements RevisionListener {
        public void newRevision(Object revisionEntity) {
            ExampleRevEntity exampleRevEntity = (ExampleRevEntity) revisionEntity;
            Identity identity = (Identity) Component.getInstance("org.jboss.seam.security.identity");
    
            exampleRevEntity.setUsername(identity.getUsername());
        }
    }
    

    So, I suppose your question would be how to retrieve the currently logged in user at that point if you don't use Seam? I'm sure there are several ways to do it, we do it like this.

    • Write a ServletFilter
    • In the filter get the principal from the session (or the security context if you use a security framework like Spring Security)
    • Store it in the log4j MDC

    Code example from filter

    protected void beforeRequest(final HttpServletRequest request, final String message) {
      final Principal principal = request.getUserPrincipal();
      final String username = principal != null ? principal.getName() : null;
      if (username != null) {
        MDC.put(USER, username);
      }
    }
    protected void afterRequest(final HttpServletRequest request, final String message) {
      MDC.remove(USER);
    }
    

    Later you can get the user from anywhere in your code because the MDC has a static get(String) method.

    这篇关于Hibernate的春天Web应用程序如何登录用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆