简体   繁体   中英

Spring Security: How to redirect to a REST url after login

I'm not sure if I know about the subject matter well enough to correctly ask the question.

Anyways, after login, I want to redirect to a url with the username in the url path. How am I able to do that?

For example, someone logs in with the user name "bmarkham". I want to redirect after login to www.website.com/bmarkham

Here is my spring-security.xml

<form-login login-page="/login" default-target-url="/welcome/"
        authentication-failure-url="/login?error" username-parameter="username"
        password-parameter="password" login-processing-url="/auth/login_check" />

I"ve tried messing with default-target-url and nothing really works.

Here is my Controller

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) {
    ModelAndView model = new ModelAndView();

    if (error != null) {
        model.addObject("error", "Invalid username and password");
    }
    model.addObject("msg", "This is a message and stuff");
    return model;
}

@RequestMapping(value = { "/welcome/{userName}", "/welcome" }, method = RequestMethod.GET, produces = "application/json")
public User welcome(@PathVariable String userName) {
    User user = new User();
    user.setEmail(userName + "@something.com");
    user.setUsername(userName);
    return user;
}

Create custom implementation of AuthenticationSuccessHandler :

package com.myapp.security;

public class RedirectLoginSuccessHandler implements AuthenticationSuccessHandler {

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

        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, 
            "www.website.com/"+authentication.getName());
    }
}

Create bean of this handler:

<bean id="successLoginHandler" class="com.myapp.security.RedirectLoginSuccessHandler" />

Register this bean in security configuration:

<form-login login-page="/login" default-target-url="/welcome/"
        authentication-failure-url="/login?error" username-parameter="username"
        password-parameter="password" login-processing-url="/auth/login_check" 
        authentication-success-handler-ref="successLoginHandler"
/>

Now, you will be redirected after login.

Note: if you would like redirect to controller method, just redirect to mapping url. For example for redirect to "/welcome/{userName}" , your code in handler will looks like following:

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

    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, 
    "/welcome/"+authentication.getName());
}

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