【问题标题】:successForwardUrl does not work with Spring Social after authenticating successfully成功认证后successForwardUrl 不能与Spring Social 一起使用
【发布时间】:2019-10-31 09:33:33
【问题描述】:

我正在开发一个与 Spring Social 集成的 Spring Boot 项目。成功通过谷歌身份验证后,我想重定向到端点/userInfo,但它似乎重定向到我请求向谷歌进行身份验证的上一页: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 url吗?

【问题讨论】:

    标签: spring-boot spring-social


    【解决方案1】:

    尝试在 POST "/userInfo" 中返回 "redirect:/userInfoPage"。

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 2021-07-30
      • 2011-11-30
      • 1970-01-01
      • 2015-03-26
      • 2021-01-27
      • 2016-04-11
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多