Solon v3.1.2

solon-web-cors

native </> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-web-cors</artifactId>
</dependency>

1、描述

基础扩展插件,为 Solon Web 提供跨域访问配置支持。

2、代码应用

以下只是示例,具体配置要按需而定。

方式一:加在控制器上,或方法上

@CrossOrigin(origins = "*")
@Controller
public class Demo1Controller {
    @Mapping("/hello")
    public String hello() {
        return "hello";
    }
}

@Controller
public class Demo2Controller {
    @CrossOrigin(origins = "*")
    @Mapping("/hello")
    public String hello() {
        return "hello";
    }
}

方式2:加在控制器基类

@CrossOrigin(origins = "*")
public class BaseController {
    
}

@Controller
public class Demo3Controller extends BaseController{
    @Mapping("/hello")
    public String hello() {
        return "hello";
    }
}

方式3:全局加在应用上

public class App {
    public static void main(String[] args) {
        Solon.start(App.class, args, app->{
            //例:增加全局处理(用过滤器模式)//对静态资源亦有效
            app.filter(-1, new CrossFilter().allowedOrigins("*")); //加-1 优先级更高
        
            //例:或者增某段路径的处理(对静态文件也有效)
            app.filter(new CrossFilter().pathPatterns("/user/**").allowedOrigins("*"));

            //例:或者增某段路径的处理(只对动态请求有效)
            app.routerInterceptor(new CrossInterceptor().pathPatterns("/user/**").allowedOrigins("*"));
        });
    }
}