Ways to get meta info of servlet in filter?

huangapple go评论72阅读模式
英文:

ways to get meta info of servlet in filter?

问题

我想以声明方式向servlet方法添加权限,例如:

// servlet
@Perms("admin", "finance")
public void doPost(ServletRequest req, ServletResponse res) {
   ...
}
// web过滤器
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
   List<String> allowedRoles = ... // 以某种方式从@Perms获取值
}

也许还有其他的方法可以在没有注解的情况下实现,这只是我想要做的思路示例。

更抽象的示例:

@WebServlet("/someaddress")
// servlet
@What("have a nice day")
public void doPost(ServletRequest req, ServletResponse res) {
   ...
}
@WebFilter("/*")
// web过滤器
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
   String msg = ... // 以某种方式从@What获取值
}
英文:

I want to add permissions to servlet methods in declarative way, for example:

// servlet
@Perms(&quot;admin&quot;, &quot;finance&quot;)
public void doPost(servletRequest req, servletResponse res) {
   ...
}
// web filter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
   List&lt;String&gt; allowedRoles = ... // somehow get values from @Perms
} 

May be there is some other ways to do it without annotations, it is just example of idea what I want to do.

Or more abstract example:

@WebServlet(&quot;/someaddress&quot;)

// servlet
@What(&quot;have a nice day&quot;)
public void doPost(servletRequest req, servletResponse res) {
   ...
}
@WebFilter(&quot;/*&quot;)
// web filter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
   String msg = ... // somehow get values from @What
} 

答案1

得分: 0

Here is the translated content:

  1. 声明注解
@Retention(RetentionPolicy.RUNTIME)
public @interface What {
    String[] value();
}
  1. 覆盖 servlet 中的 init 方法(我认为声明另一个所有 servlet 都将继承其 init 方法的类会很有用)
    @Override
    public void init() throws ServletException {
        ServletContext ctx = this.getServletContext();
        final Class[] sFormalArgs = {HttpServletRequest.class, HttpServletResponse.class};
        try {
            Method m = this.getClass().getDeclaredMethod("doGet", sFormalArgs); // 对其他方法也做同样的操作
            What a = m.getAnnotation(What.class);
            String[] value = a.value();
            ctx.setAttribute("someStuff", value);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
  1. 在方法中添加注解
    @What({"admin", "finance"})
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        ....
    }
  1. 在 web 过滤器中获取它
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain
    ) {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        ServletContext ctx = req.getServletContext();
        Object o = ctx.getAttribute("someStuff");
    }

请不要忘记处理错误和多线程问题。

英文:

Here is solution

  1. declare annotation
@Retention(RetentionPolicy.RUNTIME)
public @interface What {
    String[] value();
}
  1. override init method in servlet (I think it will be usefull to declare another class that all servlet will be inherit with that init)
    @Override
    public void init() throws ServletException {
        ServletContext ctx = this.getServletContext();
        final Class[] sFormalArgs = {HttpServletRequest.class,HttpServletResponse.class};
        try {
            Method m = this.getClass().getDeclaredMethod(&quot;doGet&quot;, sFormalArgs); // do the same with other methods
            What a = m.getAnnotation(What.class);
            String[] value = a.value();
            ctx.setAttribute(&quot;someStuff&quot;, value);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
  1. add annotation to method
    @What({&quot;admin&quot;, &quot;finance&quot;})
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        ....
    }
  1. get it in web filter
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain
    ) {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        ServletContext ctx = req.getServletContext();
        Object o = ctx.getAttribute(&quot;someStuff&quot;);
    }

do not forget to handle errors and multithreding issues

huangapple
  • 本文由 发表于 2020年8月14日 18:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定