您好我的微服务sso设置有CORS问题,但我没有弄明白为什么。我的设置:
oauth微服务端口8899:
@Configuration
@Order(-20)
public class EndpointSecurityConfig extends WebSecurityConfigurerAdapter {
private AuthenticationManager authenticationManager;
@Autowired
public EndpointSecurityConfig(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.formLogin()
.loginPage("/login")
.usernameParameter("name")
.loginProcessingUrl("/login.do").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/login.do", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
// @formatter:on
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.parentAuthenticationManager(authenticationManager);
}
}
PrincipalRestController
@RestController
public class PrincipalRestController {
@RequestMapping("/principal")
Principal principal(Principal principal) {
return principal;
}
}
zuul网关端口8765:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableOAuth2Sso
@EnableAutoConfiguration
@EnableFeignClients
public class GatewayApplication extends WebSecurityConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.logout().and()
.authorizeRequests()
.antMatchers("/index.html", "/**/*.js", "/", "/login").permitAll()
.anyRequest().authenticated()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:on
}
}
zuul config:
server:
port: 8765
spring:
aop:
proxy-target-class: true
security:
basic:
enabled: false
oauth2:
user:
password: none
client:
accessTokenUri: http://localhost:8899/uaa/oauth/token
userAuthorizationUri: http://localhost:8899/uaa/oauth/authorize
clientId: client
clientSecret: secret
resource:
userInfoUri: http://localhost:8899/uaa/principal
preferTokenInfo: false
zuul:
routes:
adminPortal:
url: http://localhost:4200
path: /**
user:
url: http://localhost:8899/uaa/principal
网关后面的angular 2 app port 4200:
服务
@Injectable()
export class LoginService {
constructor (private http: Http) {}
getLoggedInUser() : Observable<LoggedInUser>{
var authHeader = new Headers();
authHeader.append( "X-Requested-With", "XMLHttpRequest" );
return this.http.get("/user",{
headers: authHeader
})
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
logout() : Observable<LoggedInUser>{
var authHeader = new Headers();
authHeader.append( "X-Requested-With", "XMLHttpRequest" );
return this.http.post("/logout",{},{headers: authHeader})
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
}
成分</ P>
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass'],
providers: [ LoginService ]
})
export class AppComponent {
loggedInUser: LoggedInUser;
constructor(
private loginService: LoginService
){
this.getLoggedInUser();
}
getLoggedInUser(){
// Get all comments
this.loginService.getLoggedInUser()
.subscribe(
loggedInUser => this.loggedInUser = loggedInUser,
err => {
console.log(err);
});
}
logout(){
this.loginService.logout()
.subscribe(
loggedInUser => this.loggedInUser = loggedInUser,
err => {
console.log(err);
});
}
}
所以如果我去浏览器并打开localhost:8765 / request被植根到执行getLoggedInUser()调用的main angular2页面。这将转到&#39; localhost:8765 / user&#39;因为在此阶段用户未登录此调用失败,302按预期失败,但随后自动重定向到登录也抛出302然后链中的其他调用用302执行。在同一时间控制台显示
XMLHttpRequest无法加载http://localhost:8899/uaa/oauth/authorize?client_id=client&redirect_uri=http://localhost:8765/login&response_type=code&state=woE3Yc。从&#39; http://localhost:8899/uaa/oauth/authorize?client_id=client&redirect_uri=http://localhost:8765/login&response_type=code&state=woE3Yc&#39;重定向到&#39; http://localhost:8899/uaa/login&#39;被CORS政策阻止:No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; http://localhost:8765&#39;因此不允许访问。
所有这些都在下面的图片中展示:
答案 0 :(得分:0)