Tôi đã tạo hai ứng dụng web - ứng dụng khách và dịch vụ.
Sự tương tác giữa ứng dụng khách và dịch vụ diễn ra tốt đẹp khi chúng được triển khai trong cùng một cá thể Tomcat.
Nhưng khi các ứng dụng được triển khai vào các phiên bản Tomcat riêng biệt (các máy khác nhau), tôi nhận được lỗi dưới đây khi yêu cầu gửi ứng dụng dịch vụ.Vấn đề về CORS - Không có tiêu đề 'Access-Control-Allow-Origin' có trên tài nguyên được yêu cầu
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401
Ứng dụng khách của tôi sử dụng JQuery, HTML5 và Bootstrap.
gọi AJAX được thực hiện để phục vụ như hình dưới đây:
var auth = "Basic " + btoa({usname} + ":" + {password});
var service_url = {serviceAppDomainName}/services;
if($("#registrationForm").valid()){
var formData = JSON.stringify(getFormData(registrationForm));
$.ajax({
url: service_url+action,
dataType: 'json',
async: false,
type: 'POST',
headers:{
"Authorization":auth
},
contentType: 'application/json',
data: formData,
success: function(data){
//success code
},
error: function(jqXhr, textStatus, errorThrown){
alert(errorThrown);
});
}
ứng dụng dịch vụ của tôi sử dụng Spring MVC, Spring liệu JPA và Xuân An.
Tôi đã bao gồm CorsConfiguration
lớp như hình dưới đây:
CORSConfig.java
:
@Configuration
@EnableWebMvc
public class CORSConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("*");
}
}
SecurityConfig.java
:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@ComponentScan(basePackages = "com.services", scopedProxy = ScopedProxyMode.INTERFACES)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("authenticationService")
private UserDetailsService userDetailsService;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(authenticationProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().fullyAuthenticated();
http.httpBasic();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
}
Xuân An phụ thuộc:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
Tôi đang sử dụng máy chủ Apache Tomcat để triển khai . yêu cầu preflight
'.andMatchers (HttpMethod.OPTIONS, "/ **"). allowAll() 'không bao giờ làm việc cho tôi, nó không đưa ra các tiêu đề CORS thích hợp trên' OPTIONS'. Chỉ kỹ thuật CORS được Spring Security chấp thuận mới hoạt động. –
@IndraKP Nếu bạn sử dụng Bảo mật mùa xuân> = 4, thì hãy sử dụng cách khác. Nếu bạn sử dụng Spring Security 3 thì bạn phải nhập đúng lớp 'HttpMethod'. – dur