Spring Boot basic knowledge

Spring Boot How to solve cross-domain problems?
Cross-domain JSONP can be solved by the front end, but JSONP can only send a GET request can not be sent to other types of requests, in RESTful style applications, it is very sad, so we recommend backend to resolve the issue through cross-domain (CORS, Cross-origin resource sharing ). This solution is not unique to Spring Boot, in the traditional SSM framework, cross-domain problem can be solved by CORS, CORS configuration before but we are in an XML file, you can now implement the interface and then rewrite addCorsMappings method WebMvcConfigurer to solve cross-domain problems.

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }

}

Before and after the project ends deployed separately, so it is necessary to solve the problem of cross-domain.
We use the cookie to store user login information, authority control interceptors in spring, when the authority does not meet directly returned to the user fixed json result.
When a user logs on, the normal use; when users log out or token expires, because the interceptors and cross-domain sequence in question, there has been cross-domain phenomenon.
We know that an http request, go first filter, before reaching the servlet for processing interceptors, if we cors on the filter, the priority will be to execute permissions interceptors.

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

}

Spring Boot labeled the jar and the jar ordinary What is the difference?
Spring Boot packaged into the jar final project is an executable jar, this jar can be run directly by java -jar xxx.jar command, this may not be as ordinary jar the jar is dependent on other items, even if can not rely on the use of one of the classes.

Spring Boot the jar and can not be dependent on other projects, mainly different structures and ordinary jar of him. Ordinary jar package, unpacked directly is the package name, the bag is our code, and after the Spring Boot packaged into an executable jar unzip in \ BOOT-INF \ classes directory is under our code, and therefore can not be directly references. If I have to reference, you can increase the pom.xml file configuration, Spring Boot project packaged into two jar, an executable, a reference.
 

Published 438 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_37769323/article/details/104622780