[英]Spring Interceptor do not work JAVA
I have these two classes in my project. 我的项目中有这两节课。 When i use them in the config i can not submit the login form of the project. 当我在配置中使用它们时,我无法提交项目的登录表单。 After i fill in my credentials and click login i try to debug the application and realize that the form do not hit the methot which makes the post request. 在我填写完我的凭据并单击登录后,我尝试调试该应用程序,并意识到该表单没有命中要求发布请求的方法。 I have no idea why but when a remove interceptor from the config i have no problems with login. 我不知道为什么,但是当从配置中删除拦截器时,登录没有问题。 Please help. 请帮忙。
SessionInterceptor.class SessionInterceptor.class
public class SessionInterceptor extends HandlerInterceptorAdapter {
@Autowired
private SessionManager sm;
@Autowired
private MessageSourceAccessor msa;
@SuppressWarnings("deprecation")
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String return_url = request.getRequestURL().toString() + "?" + request.getQueryString();
return_url = return_url.replace(msa.getMessage("config.baseurl"), "");
if (!request.getRequestURI().contains("login")) {
if (sm.get(request) == null) {
response.sendRedirect(
msa.getMessage("config.baseurl") + "/login?return=" + URLEncoder.encode(return_url));
return false;
}
}
return true;
}
}
SessionManager.class SessionManager.class
@Component
public class SessionManager {
public static final String ProfileUser = "profileuser";
public UserModel get(HttpServletRequest request) {
HttpSession session = null;
session = request.getSession(false);
if (session == null)
return null;
return (UserModel) session.getAttribute(ProfileUser);
}
public void set(HttpServletRequest request, UserModel auth) {
HttpSession session = null;
session = request.getSession(false);
if (session == null)
return;
session.setAttribute(ProfileUser, auth);
}
public void remember(HttpServletRequest request) {
HttpSession session = null;
session = request.getSession(false);
if (session != null)
session.invalidate();
session.setMaxInactiveInterval(60 * 60 * 60);
}
public void init(HttpServletRequest request, UserModel auth, Boolean remember) {
HttpSession session = null;
session = request.getSession(false);
if (session != null)
session.invalidate();
session = request.getSession(true);
if (remember)
session.setMaxInactiveInterval(60 * 60 * 60 * 60);
else
session.setMaxInactiveInterval(1800);
session.setAttribute(ProfileUser, auth);
}
public void destroy(HttpServletRequest request) {
HttpSession session = null;
session = request.getSession(false);
if (session == null)
return;
session.removeAttribute(ProfileUser);
session.invalidate();
}
Can u provide your whole configuration. 您能提供您的整个配置吗? As a wild guess from your code : 从您的代码疯狂猜测:
if (!request.getRequestURI().contains("login")) {
if (sm.get(request) == null) {
response.sendRedirect(
msa.getMessage("config.baseurl") + "/login?return=" + URLEncoder.encode(return_url));
return false;
}
}
Now if your sm.get(request) == null
then the redirection will happen, thus call will not go to controller. 现在,如果您的sm.get(request) == null
,则将发生重定向,因此调用不会转到控制器。
Redirection means call again go to client, from where a new call will be initiated to the said url. 重定向意味着再次将呼叫转到客户端,从该客户端将发起一个新呼叫到该URL。
OR 要么
if your @Autowire
is not working in that case sm
will be null. 如果您的@Autowire
在这种情况下不起作用,则sm
为null。 and sm.get(request) == null
will throw NullPointerException
. 和sm.get(request) == null
将抛出NullPointerException
。 Again call will not go to controller. 再次调用将不会转到控制器。
But to sure, provide your configuration. 但是请确保提供您的配置。 OR debug your code in interceptor. 或在拦截器中调试代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.