【发布时间】:2022-09-27 09:22:08
【问题描述】:
我有一个 Java11 spring boot Angular 13 应用程序,并且我有 open id connect 身份验证。
因此,在前面,我有令牌并将其添加到我的请求中,在后面我有应用程序的安全性,但我在 TRACE 级别时没有日志,但我没有没有登录前台电话。
所以不知道是什么阻碍了前端和java后端的通信。
通信 back-OAuth ok,front-OAuth ok
OAuth2 模式是
请求前面的隐式授权。
Request URL: http://localhost:8080/api/domains/sync
Referrer Policy: strict-origin-when-cross-origin
Provisional headers are shown
Learn more
Accept: application/json, text/plain, */*
Authorization: Bearer [object Object]
Content-Type: application/json
Referer: http://localhost:4200/
sec-ch-ua: \"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: \"Windows\"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Request URL: http://localhost:8080/api/domains/sync
Referrer Policy: strict-origin-when-cross-origin
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9
Access-Control-Request-Headers: authorization,content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:8080
Origin: XXX
Referer: http://localhost:4200/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
角
@Injectable({
providedIn: \'root\',
})
export class TokenInterceptorService implements HttpInterceptor {
constructor(private authService: InitialAuthService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const token = this.authService.decodedAccessToken? this.authService.decodedAccessToken:null;
if (typeof token != \'undefined\' && token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ` + token,
},
});
console.debug(\"new request =>\");
console.debug(request);
}
return next.handle(request).pipe(
catchError((err) => {
console.error(err);
if (err.status === 401) {
}
const error = err.error.message || err.statusText;
return throwError(error);
})
);
}
}
爪哇
@Configuration
public class OAuth2SecurityConfig {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges ->
exchanges
.pathMatchers(\"/\", \"/error\").permitAll()
.anyExchange().authenticated()
)
.oauth2Login((Customizer<ServerHttpSecurity.OAuth2LoginSpec>) withDefaults());
return http.build();
}
}
@Controller
public class UserController {
@GetMapping(\"/\")
public String index() {
return \"index\";
}
@GetMapping(\"/user\")
public String user(Model model,
@AuthenticationPrincipal OidcUser oidcUser) {
model.addAttribute(\"userName\", oidcUser.getName());
model.addAttribute(\"audience\", oidcUser.getAudience());
return \"user\";
}
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>