[英]Adding parameters to Spring MVC Interceptor
I'm trying to achieve authorization for my RestAPI, by letting Spring's Interceptor's ( HandlerInterceptorAdapter ) PreHandle method check wether the user is in the required role, before the scope hits the requested action in the controller. 我正在尝试通过让Spring的Interceptor的( HandlerInterceptorAdapter )PreHandle方法检查用户是否处于所需角色中,从而使我的RestAPI获得授权,以使作用域在控制器中达到所请求的操作。 This requires, however, that I provide each action (URL Path) with the ID of the role it requires. 但是,这要求我为每个操作(URL路径)提供所需角色的ID。 This is my current setup: 这是我当前的设置:
public class AuthorizationInterceptor extends HandlerInterceptorAdapter{
@Autowired
IUserService us;
//before the actual handler will be executed
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler, Integer roleId)
throws Exception {
String userId = request.getHeader("UserId");
if(!us.isUserInRole(Long.parseLong(userId), roleId))
return false;
return true;
}
}
And (a part of) the servlet-context.xml: 以及servlet-context.xml(的一部分):
<interceptors>
<interceptor>
<mapping path="/" />
<mapping path="/users/**" />
<beans:bean class="com.lumi.api.interceptors.AuthorizationInterceptor"></beans:bean>
</interceptor>
</interceptors>
My question is, wether I can pass in the parameter roleId with the bean in the servlet-context config. 我的问题是,是否可以在servlet上下文配置中将参数roleId与bean一起传递。 I can't seem to find anything in the docs. 我似乎在文档中找不到任何内容。 I think I once saw something like: 我想我曾经看到过类似的东西:
<mapping path="/" />
<parameter name="something" value="some value">
But i'm not sure. 但是我不确定。
You can simply set a property using a standard spring sintax, an example 您可以简单地使用标准spring sintax设置属性,例如
<beans:bean class="com.lumi.api.interceptors.AuthorizationInterceptor">
<beans:property name="roleId" value="REGISTERED_USER"/>
</beans:bean>
your interceptor should of course include the property, so simply 您的拦截器当然应该包含该属性,因此很简单
public class AuthorizationInterceptor extends HandlerInterceptorAdapter{
private String roleId;
public String getRoleId() {
return roleId;
}
public void setRoleId(String roleId) {
this.roleId = roleId;
}
// The rest of your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.