简体   繁体   中英

Redirect url in Spring security

Good morning,

I´ve been looking my Spring security configuration, I´m wondering how I could redirect to a specific url all paths that are pointing to for example /foo/*. I need to do something similar as we do when there´s no session, and we redirect to the login page.

In my case I need that whatever url that is pointing to /foo/* would be redirect to a in_development_page.html.

I would like to use the intercept pattern if would be possible, something like this.

   <intercept-url pattern="/foo/*" redirect="in_development_page.html"/>

Do you know if something like this it´s possible?.

Regards.

You can use interceptor for it :

<mvc:interceptor>
    <mvc:mapping path="/foo/**"/>            
    <bean class="com.interceptors.AuthorizationInterceptor" />
</mvc:interceptor>

suppose we want to check session then you can write code in AuthorizationInterceptor like :

    if(UserSession.isSessionValid()){
        System.out.println("ready to enter");
        return true;
    }else{
        System.out.println("not logged in...redirecting to login");
        response.sendRedirect("/in_development_page");
        return false;
    }

here UserSession is my class that have session variables.

if you just want to redirect then you can remove code of if & only pass response.sendRedirect() . in parameter pass the page URL of under development.

full class code :

public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private IUserSession UserSession;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("interceptor called for " + request.getRequestURI());
        if(UserSession.isSessionValid()){
            System.out.println("ready to enter");
            return true;
        }else{
            System.out.println("not logged in...redirecting to login");
            response.sendRedirect("/in_development_page");
            return false;
        }
    }
}

here class will extend HandlerInterceptorAdapter spring class.

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