successForwardUrl не работает с Spring Social после успешной аутентификации - PullRequest
0 голосов
/ 17 июня 2019

Я работаю над проектом Spring Boot, интегрирующимся с Spring Social. После успешной аутентификации в Google я хочу перенаправить на конечную точку /userInfo, но она, кажется, перенаправляет на предыдущую страницу, где я делаю запрос на аутентификацию в Google: http://localhost:8080/auth/google

Я также пытался создать bean-компонент CustomAuthenticationSuccessHandler, который реализует AuthenticationSuccessHandler и добавить его в файл конфигурации, но он также не работал

Моя конфигурация WebSecurity:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    DataSource dataSource;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    // This bean is load the user specific data when form login is used.
    @Override
    public UserDetailsService userDetailsService() {
        return userDetailsService;
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    // Enable jdbc authentication
    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .passwordEncoder(passwordEncoder())
                .usersByUsernameQuery("select user_name, encryted_password"
                        + " from app_user where user_name=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();

        // Pages do not require login
        http.authorizeRequests()
                .antMatchers("/", "/signup", "/login", "/logout")
                .permitAll();

        http.authorizeRequests()
                .antMatchers("/user/**")
                .access("hasRole('" + AppRole.ROLE_USER + "')");

        // For ADMIN only.
        http.authorizeRequests()
                .antMatchers("/admin/**")
                .access("hasRole('" + AppRole.ROLE_ADMIN + "')");

        // When the user has logged in as XX.
        // But access a page that requires role YY,
        // AccessDeniedException will be thrown.
        http.authorizeRequests()
                .and()
                .exceptionHandling()
                .accessDeniedPage("/403");

        // Form Login config
        http.authorizeRequests()
                .and()
                .formLogin()
                .loginProcessingUrl("/j_spring_security_check") // the url to submit the username and password to
                .loginPage("/login") // the custom login page
                .successForwardUrl("/userInfo") // the landing page after a successful login
                .failureUrl("/login?error=true") // the landing page after an unsuccessful login
                .usernameParameter("username")
                .passwordParameter("password");

        // Logout Config
        http.authorizeRequests()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/logoutSuccessful");


        http.apply(new SpringSocialConfigurer())
                .signupUrl("/signup");

    }

}

MyController:

@RestController
@Transactional
public class MainController {

    @Autowired
    private AppUserDAO appUserDAO;

    @Autowired
    private ConnectionFactoryLocator connectionFactoryLocator;

    @Autowired
    private UsersConnectionRepository userConnectionRepository;

    @Autowired
    private AppUserValidator appUserValidator;

    @RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
    public String welcomePage(Model model) {
        model.addAttribute("title", "Welcome");
        model.addAttribute("message", "This is welcome page!");
        return "welcomePage";
    }

    @RequestMapping(value = "/logoutSuccessful", method = RequestMethod.GET)
    public String logoutSuccessfulPage(Model model) {
        model.addAttribute("title", "Logout");
        return "logoutSuccessfulPage";
    }

    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    public String userInfo(Model model, Principal principal) {

        // After user login successfully.
        String userName = principal.getName();

        System.out.println("User Name: " + userName);

        UserDetails loginedUser = (UserDetails) ((Authentication) principal).getPrincipal();

        String userInfo = WebUtils.toString(loginedUser);
        model.addAttribute("userInfo", userInfo);

        return "userInfoPage";
    }

Есть ли способы переадресации на /userInfo URL после входа в Spring Social?

...