简体   繁体   English

在Spring Security中重定向URL

[英]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/*. 我一直在寻找我的Spring安全配置,我想知道如何将所有指向/ foo / *的路径重定向到特定的url。 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. 就我而言,我需要将指向/ foo / *的所有URL重定向到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 : 假设我们要检查会话,则可以在AuthorizationInterceptor编写代码,例如:

    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. 这里的UserSession是我的具有会话变量的类。

if you just want to redirect then you can remove code of if & only pass response.sendRedirect() . 如果您只想重定向,则可以删除if和仅通过response.sendRedirect() in parameter pass the page URL of under development. 参数中传递正在开发的页面URL

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. 这里的类将扩展HandlerInterceptorAdapter弹簧类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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