英文:
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("admin", "finance")
public void doPost(servletRequest req, servletResponse res) {
...
}
// web filter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
List<String> 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("/someaddress")
// servlet
@What("have a nice day")
public void doPost(servletRequest req, servletResponse res) {
...
}
@WebFilter("/*")
// 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:
- 声明注解
@Retention(RetentionPolicy.RUNTIME)
public @interface What {
String[] value();
}
- 覆盖 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();
}
}
- 在方法中添加注解
@What({"admin", "finance"})
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
....
}
- 在 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
- declare annotation
@Retention(RetentionPolicy.RUNTIME)
public @interface What {
String[] value();
}
- override
init
method in servlet (I think it will be usefull to declare another class that all servlet will be inherit with thatinit
)
@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); // do the same with other methods
What a = m.getAnnotation(What.class);
String[] value = a.value();
ctx.setAttribute("someStuff", value);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
- add annotation to method
@What({"admin", "finance"})
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
....
}
- 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("someStuff");
}
do not forget to handle errors and multithreding issues
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论