Skip to content

Commit

Permalink
去除跨域cors
Browse files Browse the repository at this point in the history
  • Loading branch information
fangp committed Jun 25, 2018
1 parent 1616381 commit 57e36c0
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 139 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# spring-cloud-base
微服务基础框架,基于 SpringCloud 及 SpringBoot 开发。 使用 Oauth2 统一授权、认证, Oauth示例客户端使用 Vue 开发,具有用户管理、 资源管理、 角色管理等模块,后端包括授权中心、 基础数据中心(资源服务器)等应用,可作为微服务快速开发脚手架。 可通过 docker 快速构建部署。

Demo website:http://112.74.60.248:8080/
Demo website:http://www.mxclass.cn
username: test password: 123456
phone: 13100000000 verifyCode: 1000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class ApiGatewayApplication {
public static void main(String[] args){
SpringApplication.run(ApiGatewayApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@

/**
* Created by fp295 on 2018/5/6.
* 跨域开发环境使用 vue-cli 代理,正式用nginx
*/
@Order(2)
@Configuration
public class CorsConfig {

@Bean
/*@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
source.registerCorsConfiguration("*", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}*/
}
2 changes: 1 addition & 1 deletion api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server:
zuul:
routes:
main-data:
path: /main-data/api/**
path: /api/main-data/**
serviceId: main-data
proxy:
auth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* Created by fp295 on 2018/4/29.
* 包装org.springframework.security.core.userdetails.User类
* 新增 baseUser 用于生成 jwt 的用户信息
*/
public class BaseUserDetail implements UserDetails, CredentialsContainer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ public JwtAccessTokenConverter jwtAccessTokenConverter() {


/**
* 跨域
* 跨域, 开发环境使用 vue-cli 代理,正式用nginx
*/
@Bean
/*@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
source.registerCorsConfiguration("*//**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
/**
* Created by fp295 on 2018/6/16.
* 自定义登陆filter,新增登陆方式:验证码、二维码扫码、账号密码;
* 验证码登陆:
* post: /login?type=phone&phone=13000000000&verifyCode=1000
* 二维码登陆:
* post: /login?type=qr&qrCode=token
* 账号密码登陆:
* post: /login?username=username&password=password
* 此filter 为生成自定义的 MyAuthenticationToken
*/
public class MyLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected final UserDetails retrieveUser(String username, MyAuthenticationToken
UserDetails loadedUser;
try {
// 调用loadUserByUsername时加入type前缀
loadedUser = this.getUserDetailsService().loadUserByUsername(authentication.getType() + ":" + username);
loadedUser = this.getUserDetailsService().loadUserByUsername(authentication.getType() + "&:@" + username);
} catch (UsernameNotFoundException var6) {
if(authentication.getCredentials() != null) {
String presentedPassword = authentication.getCredentials().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ModelAndView authorizePage(Map<String, Object> model) {
}

/**
* 主页
* 主页,未从客户端跳转直接登陆会显示
* @param model
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

/**
* ftl视图
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/login").setViewName("login");
//registry.addViewController("/").setViewName("authorize");
}

/**
* 静态资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ public class BaseUserDetailService implements UserDetailsService {
public UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException {

BaseUser baseUser;
String[] parameter = var1.split(":");
String[] parameter;
int index = var1.indexOf("&:@");
if (index != -1) {
parameter = var1.split("&:@");
}else {
// 如果是 refresh_token 不分割
parameter = new String[]{MyLoginAuthenticationFilter.SPRING_SECURITY_RESTFUL_TYPE_DEFAULT, var1};
}

// 手机验证码调用FeignClient根据电话号码查询用户
if(MyLoginAuthenticationFilter.SPRING_SECURITY_RESTFUL_TYPE_PHONE.equals(parameter[0])){
ResponseData<BaseUser> baseUserResponseData = baseUserService.getUserByPhone(parameter[1]);
Expand All @@ -70,6 +78,7 @@ public UserDetails loadUserByUsername(String var1) throws UsernameNotFoundExcept
baseUser = baseUserResponseData.getData();
}


// 调用FeignClient查询角色
ResponseData<List<BaseRole>> baseRoleListResponseData = baseRoleService.getRoleByUserId(baseUser.getId());
List<BaseRole> roles;
Expand Down
11 changes: 10 additions & 1 deletion web-app/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ module.exports = {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
proxyTable: {
'/api':{
target: 'http://127.0.0.1:18000',
changeOrigin: true // 必须,为true的话,请求的header将会设置为匹配目标服务器的规则(Access-Control-Allow-Origin)
},
'/auth':{
target: 'http://127.0.0.1:18001',
changeOrigin: true // 必须,为true的话,请求的header将会设置为匹配目标服务器的规则(Access-Control-Allow-Origin)
}
},

// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
Expand Down
Loading

0 comments on commit 57e36c0

Please sign in to comment.