问题描述
目前,当您使用通过应用程序创建的帐户登录时 - 您将获得 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/