Annotation of Cors to solve cross-domain problems

SpringMVC4.2 began to add a cross solution, which is explained here by adding an annotation @CrossOrigin in the controller

  1. Check the source code of @CrossOrigin to understand that this annotation integrates the relevant configuration of cross-domain configuration by default, just add some parameters to this annotation
package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CrossOrigin {
    /** @deprecated */
    @Deprecated
    String[] DEFAULT_ORIGINS = new String[]{"*"};
    /** @deprecated */
    @Deprecated
    String[] DEFAULT_ALLOWED_HEADERS = new String[]{"*"};
    /** @deprecated */
    @Deprecated
    boolean DEFAULT_ALLOW_CREDENTIALS = false;
    /** @deprecated */
    @Deprecated
    long DEFAULT_MAX_AGE = 1800L;

    @AliasFor("origins")
    String[] value() default {};

    @AliasFor("value")
    String[] origins() default {};

    String[] allowedHeaders() default {};

    String[] exposedHeaders() default {};

    RequestMethod[] methods() default {};

    String allowCredentials() default "";

    long maxAge() default -1L;
}
  1. If the default configuration is used, it can be seen that all domain names, request headers, request methods, cookies,
  2. If you need to specify the cross-domain domain name or other parameters can be added in the comment, for example. Other parameters can also be added
@CrossOrigin(value = "http://www.twgfs.online",maxAge = 30,methods=RequestMethod.GET)
Published 19 original articles · praised 7 · visits 6625

Guess you like

Origin blog.csdn.net/William_TWG/article/details/104976086