//application.properties文件
#actuator
endpoints.health.sensitive=true
endpoints.health.enabled=true
endpoints.metrics.enabled=true
endpoints.metrics.sensitive=true
endpoints.info.id=info
endpoints.info.sensitive=false
endpoints.info.enabled=true
monitor.username=admin
monitor.password=admin
monitor.role=USER
endpoints.health.mapping.DOWN= INTERNAL_SERVER_ERROR
management.security.enabled=true
management.contextPath= /actuator
management.security.roles=USER
app.version=@app_version@
app.product=@app_product@
app.artifact=@app_artifact@
//gradle.properties文件
# ${PRODUCT} 该占位符表示产品线,一般与gerrit项目所在前缀相同,如abc/live,abc表示产品线
# ${ARTIFACT} 该占位符表示组件名,如abc/live,live表示组件名
# ${VERSION} 该占位符表示版本号,由三位数字构成x.x.x ,如果是master线上版本则版本号后缀添加小写 -release,如果是rc-x.x.x开发版本则版本号后缀添加小写 -snapshot
version=1.1.0-RELEASE
product=word-assistant
artifact=manage-platform
//build.gradle文件,这段grovvy代码是为了获取gradle.properties文件中的值
processResources {
filesMatching(‘application.properties’) {
filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [app_version: project.property(‘version’),app_product: project.property(‘product’),app_artifact: project.property(‘artifact’)]
}
}
//配置权限的文件
/**
* @Author:
* @Description: 配置登录接口以及监视接口的权限,监视接口采用http basic进行认证
* @Date: 上午9:28 2017/7/19
* *Params
*/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(this.userDetailsService);
// .passwordEncoder(passwordEncoder());
}
@Configuration
@Order(1)//配置加载的顺序,值越大优先级越低
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Value("${monitor.username}")
private String username;
@Value("${monitor.password}")
private String password;
@Value("${monitor.role}")
private String role;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser(username)
.password(password).roles(role);
}
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/actuator/*")
.authorizeRequests()
.anyRequest().hasRole(role)
.and()
.httpBasic();
}
}
@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Bean
public TokenFilter authenticationTokenFilterBean() throws Exception {
return new TokenFilter();
}
@Autowired
private WebAuthenticationEntryPoint unauthorizedHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers(HttpMethod.GET,
"/",
"/*.html",
"/favicon.ico",
"/**/*.html",
"/**/*.css",
"/**/*.png",
"/**/*.psd",
"/**/*.woff",
"/**/*.woff2",
"/**/*.ttf",
"/vendor/**",
"/**/*.json",
"/**/*.js").permitAll()
.antMatchers("/api/account/login","/api/login").permitAll()
.anyRequest().authenticated();
// 添加JWT filter
http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
// 禁用缓存
http.headers().cacheControl();
}
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
/**
* Created by * on 2017/7/12.
*/
@Component(“manage-platform-health”)
public class ApplicationHealth implements HealthIndicator {
protected static final Logger log = LoggerFactory.getLogger(ApplicationHealth.class);
@Value("${monitor.username}")
private String username;
@Value("${monitor.password}")
private String password;
@Autowired
private FixedSentenceDAOImpl fixedSentenceDAO;
@Override
public Health health() {
try {
fixedSentenceDAO.queryFixedSentenceByPage(0, 5);
} catch(Exception e) {
return Health.down().withDetail("exception", e.getMessage()).build();
}
return Health.up().build();
}
}
/**
* @Author:
* @Description: info接口,在actuator中用来显示程序的版本号、项目名称、属于哪个产品线
* @Date: 下午5:29 2017/7/19
* *Params
*/
@Component
public class ApplicationInfo implements InfoContributor {
@Value("${app.version}")
private String version;
@Value("${app.product}")
private String product;
@Value("${app.artifact}")
private String artifact;
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("version", version).withDetail("product", product).withDetail("artifact", artifact);
}
}
/**
* Created by * on 2017/7/14.
*/
@Component
public class ElasticsearchHealth extends AbstractHealthIndicator {
@Autowired
private ElasticCntPool elasticCntPool;
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
TransportClient client = elasticCntPool.getEsClientInstance();
//client.admin().cluster().health();
ClusterHealthResponse response = client.admin().cluster()
.health(Requests.clusterHealthRequest()).actionGet();
switch (response.getStatus()) {
case GREEN:
case YELLOW:
builder.up();
break;
case RED:
default:
builder.down();
break;
}
builder.withDetail("clusterName", response.getClusterName())
.withDetail("numberOfNodes", response.getNumberOfNodes())
.withDetail("numberOfDataNodes", response.getNumberOfDataNodes());
}
}