Spring Security:如何获取初始目标URL

时间:2009-03-10 16:50:45

标签: spring spring-security

我正在使用弹簧安全来限制网址。我正在尝试在同一页面上提供注册和登录页面。

登录弹簧安全性转移到受限页面。但是我试图将目标网址传递给注册过程,以便在注册后我们可以重定向到受限制的页面。

如何获取用户重定向的实际URL。

任何想法?

4 个答案:

答案 0 :(得分:14)

这就是我从Spring Security获取URL的方式。

SavedRequest savedRequest = (SavedRequest)session.getAttribute(
    AbstractProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY);
String requestUrl = savedRequest.getFullRequestUrl();

答案 1 :(得分:13)

他们在spring security 3.0中移动了一些东西,所以上面的代码片段不再起作用了。不过这样做很好:

protected String getRedirectUrl(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    if(session != null) {
        SavedRequest savedRequest = (SavedRequest) session.getAttribute(WebAttributes.SAVED_REQUEST);
        if(savedRequest != null) {
            return savedRequest.getRedirectUrl();
        }
    }

    /* return a sane default in case data isn't there */
    return request.getContextPath() + "/";
}

答案 2 :(得分:3)

使用spring security 4.1.4:

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {

    SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
    if (savedRequest != null) {
        response.sendRedirect(savedRequest.getRedirectUrl());
    }
    else{
        response.sendRedirect("some/path");
    }
}

答案 3 :(得分:1)

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.crazy.tuhin.aroundme"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 5
        versionName "1.4"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    //google
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.android.gms:play-services-places:17.0.0'


}