天天看點

springCloud初探

最近公司因為受夠了外部供應商的架構,提出了使用新架構的想法,然後我們就付諸實踐了。在目前市面上的看了一圈,最後決定使用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>
           

本篇暫時隻講這兩個問題。

附上源碼:源碼位址

繼續閱讀