微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 facebook 和 gmail 登录

如何解决通过 facebook 和 gmail 登录

我有一个基于 JWT 的应用程序。

目前,当您使用通过应用程序创建的帐户登录时 - 您将获得 jwt 令牌:

@PostMapping("/authenticate")
    fun createAuthenticationToken(@RequestBody authenticationRequest: AuthenticationRequest): ResponseEntity<AuthenticationResponse> {
            authenticationManager.authenticate(
                    UsernamePasswordAuthenticationToken(authenticationRequest.username,authenticationRequest.password)
            )

        val userDetails: UserDetails = userDetailsService
                .loadUserByUsername(authenticationRequest.username)

        val jwt: String = jwtOperations.generatetoken(userDetails)

        return ResponseEntity.ok(AuthenticationResponse(jwt))
    }

我在另一个航点的@RequestHeader 内撤回。我还想通过 facebook 和 gmail 提供日志记录,我想知道如何实现。

我目前的配置:

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
class SecurityConfiguration(
        private val userDetailsService: UserDetailsService,private val jwtRequestFilter: JwtRequestFilter
) : WebSecurityConfigurerAdapter() {

    override fun configure(auth: AuthenticationManagerBuilder) {
        auth.userDetailsService(userDetailsService)
    }

    override fun configure(httpSecurity: HttpSecurity) {
        httpSecurity.csrf().disable()
                .authorizeRequests().antMatchers(...).permitAll()
                .anyRequest().authenticated()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling().authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))

        httpSecurity.addFilterBefore(jwtRequestFilter,UsernamePasswordAuthenticationFilter::class.java)
    }

    @Bean
    fun passwordEncoder(): BCryptPasswordEncoder = BCryptPasswordEncoder(10)

    @Bean
    override fun authenticationManagerBean(): AuthenticationManager = super.authenticationManagerBean()

JWTRequestfilter:

@Component
class JwtRequestFilter(
        @Qualifier("...") private val userDetailsService: UserDetailsService,private val jwtOperations: JwtOperations
) : OncePerRequestFilter() {

    override fun doFilterInternal(request: HttpServletRequest,response: HttpServletResponse,chain: FilterChain) {
        val authorizationHeader = request.getHeader("Authorization")

        var username: String? = null
        var jwt: String? = null

        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            jwt = authorizationHeader
            username = jwtOperations.extractUsername(jwt)
        }

        if (username != null && SecurityContextHolder.getContext().authentication == null) {
            val userDetails: UserDetails = userDetailsService.loadUserByUsername(username)

            if (jwtOperations.validatetoken(jwt,userDetails)) {
                val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(
                        userDetails,null,userDetails.authorities)
                usernamePasswordAuthenticationToken.details = WebAuthenticationDetailsSource().buildDetails(request)
                SecurityContextHolder.getContext().authentication = usernamePasswordAuthenticationToken
            }
        }
        chain.doFilter(request,response)
    }
}

当有人完成使用 facebook 登录时,我需要以某种方式生成 JWT 令牌。我发现的所有教程都是关于在客户端(前端)与例如 facebook 集成。所以我的问题是,我怎样才能做到这一点?我如何提供使用 facebook/gmail 的登录,以便用户不必通过应用程序创建帐户。 你有任何教程或示例项目吗?我使用的是 Spring Boot 2.4.0 和 Kotlin,但我也可以阅读 Java 教程。

感谢您的建议。

解决方法

我使用下面的链接创建了我的 OAuth2。

面临的问题是

  • 无法收到 Github 用户的电子邮件
  • Apple 登录尚未实现。

除此之外,这个 repo 对我帮助很大!!

https://github.com/callicoder/spring-boot-react-oauth2-social-login-demo

说明

https://www.callicoder.com/spring-boot-security-oauth2-social-login-part-1/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。