最近公司因为受够了外部供应商的框架,提出了使用新框架的想法,然后我们就付诸实践了。在目前市面上的看了一圈,最后决定使用springCloud。
springCloud相关文章现在在各种论坛,博客上的数不胜数。我这里也就不再继续说了。文末我会把我自己做的一个小项目源码附上,里面包含注册中心,网关,监控还有一个业务项目(使用了mybatis plus),配置中心我们没有使用。这个项目源码很小但是很实用,可以直接作为后端的雏形,根据自己的需要继续添加就可以了。
这里我就讲一讲我们在整个过程中遇到的一些问题。给后续可能会使用的小伙伴们一个参考。
第一个问题,我们在项目一开始使用了oauth 2.0。但是我们又有很多接口是不需要验证用户token的,这是时候我们需要做的就是将某种格式下的访问url加入到我们的忽略路径就可以了。下面的代码给你们打个样。只要继承ResourceServerConfigurerAdapter类再将匹配我们定义的url链接规则写一下就好了。源码附上,这里我们要过滤的是mybatisPlus开头的url接口链接。
import org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
/**
* Created by zhouhaishui on 2018/5/23.
*/
@Configuration
@EnableResourceServer
public class CommunityServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.
csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\""))
.and()
.authorizeRequests()
.antMatchers("/mybatisPlus/*/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
第二个问题:我们使用的Intellij IDEA 在集成mybatis plus的时候mapper文件发生找不到自定义方法的情况。这时候我选择的是将mapper放到resource文件夹下。同时在打包的时候我们需要把xml文件打到jar包内。使用maven管理jar包的配置如下:
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
本篇暂时只讲这两个问题。
附上源码:源码地址