成功转发网址在成功验证后不适用于Spring Social



我正在做一个与Spring Social集成的Spring Boot项目。成功使用 google 进行身份验证后,我想重定向到终点/userInfo,但它似乎重定向到我请求向 Google 进行身份验证的上一页:http://localhost:8080/auth/google

我还尝试创建一个 bean CustomAuthenticationSuccessHandler ,它实现了AuthenticationSuccessHandler并将其添加到配置文件中,但它也没有工作

我的网络安全配置:

@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");
    }
}

我的控制器:

@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";
    }

使用Spring Social登录后,有什么方法可以转发到/userInfo网址吗?

尝试在

POST "/userInfo" 中返回 "redirect:/userInfoPage"。

最新更新