Ways to get meta info of servlet in filter?

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

ways to get meta info of servlet in filter?

问题

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

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

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

更抽象的示例:

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

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

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

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:

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

答案1

得分: 0

Here is the translated content:

  1. 声明注解
  1. @Retention(RetentionPolicy.RUNTIME)
  2. public @interface What {
  3. String[] value();
  4. }
  1. 覆盖 servlet 中的 init 方法(我认为声明另一个所有 servlet 都将继承其 init 方法的类会很有用)
  1. @Override
  2. public void init() throws ServletException {
  3. ServletContext ctx = this.getServletContext();
  4. final Class[] sFormalArgs = {HttpServletRequest.class, HttpServletResponse.class};
  5. try {
  6. Method m = this.getClass().getDeclaredMethod("doGet", sFormalArgs); // 对其他方法也做同样的操作
  7. What a = m.getAnnotation(What.class);
  8. String[] value = a.value();
  9. ctx.setAttribute("someStuff", value);
  10. } catch (NoSuchMethodException e) {
  11. e.printStackTrace();
  12. }
  13. }
  1. 在方法中添加注解
  1. @What({"admin", "finance"})
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
  4. ....
  5. }
  1. 在 web 过滤器中获取它
  1. @Override
  2. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain
  3. ) {
  4. HttpServletRequest req = (HttpServletRequest) servletRequest;
  5. ServletContext ctx = req.getServletContext();
  6. Object o = ctx.getAttribute("someStuff");
  7. }

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

英文:

Here is solution

  1. declare annotation
  1. @Retention(RetentionPolicy.RUNTIME)
  2. public @interface What {
  3. String[] value();
  4. }
  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)
  1. @Override
  2. public void init() throws ServletException {
  3. ServletContext ctx = this.getServletContext();
  4. final Class[] sFormalArgs = {HttpServletRequest.class,HttpServletResponse.class};
  5. try {
  6. Method m = this.getClass().getDeclaredMethod(&quot;doGet&quot;, sFormalArgs); // do the same with other methods
  7. What a = m.getAnnotation(What.class);
  8. String[] value = a.value();
  9. ctx.setAttribute(&quot;someStuff&quot;, value);
  10. } catch (NoSuchMethodException e) {
  11. e.printStackTrace();
  12. }
  13. }
  1. add annotation to method
  1. @What({&quot;admin&quot;, &quot;finance&quot;})
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  4. {
  5. ....
  6. }
  1. get it in web filter
  1. @Override
  2. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain
  3. ) {
  4. HttpServletRequest req = (HttpServletRequest) servletRequest;
  5. ServletContext ctx = req.getServletContext();
  6. Object o = ctx.getAttribute(&quot;someStuff&quot;);
  7. }

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:

确定