在应用程序中为Google登录禁用Spring Security

时间:2018-07-09 05:39:46

标签: spring spring-boot authentication spring-security spring-security-oauth2

我有一个具有默认级别Spring安全性的Spring Boot应用程序,并且我将 auth.userDetailsS​​ervice(customuserservice)用于用户身份验证部分。除此之外,我们使用Google登录按钮。我的配置文件是

`http
            .authorizeRequests()
            .antMatchers("/","/securitylogin").permitAll()
            .antMatchers("/SecondMainPage")
            .hasAnyRole("SUPERADMIN","ADMIN","USER")
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/securitylogin") //login
            .usernameParameter("username") //optional
            .passwordParameter("password") //optional
            .defaultSuccessUrl("/loggedin")
            .permitAll()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/")
            .invalidateHttpSession(true)
            .permitAll();`

身份验证部分正在按预期方式运行,但Google登录部分无法正常运行。在Google登录后,我收到了电子邮件,并从服务器获取了他的详细信息,然后重定向到一个控制器,但是该控制器重新加载了n次。我不知道为什么单一方法一次又一次地重新加载。

  

http.antMatcher(“ / loginwithgoogle / **”)。anonymous();

如果我使用上面的行,则Google登录名部分运行正常,但是普通登录身份验证无法访问。

  

web.ignoring()。antMatchers(HttpMethod.POST,“ / loginwithgoogle”);

我也尝试了上面的方法,但是同一控制器一次又一次地重新加载了n次。请帮助我继续前进。我还有什么想念的吗?还是我走错了路?

1 个答案:

答案 0 :(得分:0)

实际上,我之前尝试过相同的事情,但是不知道为什么我没有成功,但是以下事情对我有用,并且如果对任何人有帮助,也会更新相同的内容。

http
            .authorizeRequests()
            .antMatchers("/loginwithgoogle/**").anonymous() //works this order
            .anyRequest().authenticated() // with this line too
            .antMatchers("/","/securitylogin").permitAll()

我不知道是否会有单独的命令,但是我分别尝试了anonymous(),但没有成功。以上工作完美。

更新11.09.2018 Google登录正常,但是角色不起作用(即,用户可以访问管理页面,并且异常处理无法正常工作)。

相关问题