简体   繁体   中英

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

 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

@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.

Redirection means call again go to client, from where a new call will be initiated to the said url.

OR

if your @Autowire is not working in that case sm will be null. and sm.get(request) == null will throw NullPointerException . Again call will not go to controller.

But to sure, provide your configuration. OR debug your code in interceptor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM