因为注册中心基本上都是自己的应用在使用,应用不是特别多,可以写死,如果应用很多,那么就写入数据库把

pom

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-security</artifactId>
  4. </dependency>
  1. WebSecurityConfigurerAdapter ,注意为了可以使用 http://pc:123456@localhost:8000/eureka/ 这种方式登录,所以必须是httpBasic,如果是form方式,不能使用url格式登录
  1. @EnableWebSecurity
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3.  
  4. @Value("${users.admin.name}")
  5. private String admin_name;
  6. @Value("${users.admin.password}")
  7. private String admin_password;
  8. @Value("${users.admin.roles}")
  9. private String [] admin_roles;
  10.  
  11. @Value("${users.pc.name}")
  12. private String pc_name;
  13. @Value("${users.pc.password}")
  14. private String pc_password;
  15. @Value("${users.pc.roles}")
  16. private String [] pc_roles;
  17.  
  18. @Value("${users.app.name}")
  19. private String app_name;
  20. @Value("${users.app.password}")
  21. private String app_password;
  22. @Value("${users.app.roles}")
  23. private String [] app_roles;
  24.  
  25. @Value("${users.apiuser.name}")
  26. private String apiuser_name;
  27. @Value("${users.apiuser.password}")
  28. private String apiuser_password;
  29. @Value("${users.apiuser.roles}")
  30. private String [] apiuser_roles;
  31.  
  32. @Value("${users.zuul.name}")
  33. private String zuul_name;
  34. @Value("${users.zuul.password}")
  35. private String zuul_password;
  36. @Value("${users.zuul-router.roles}")
  37. private String [] zuul_roles;
  38.  
  39. @Override
  40. public void configure(WebSecurity web) throws Exception {
  41. //这里忽略app调用的接口服务,让接口服务的Oauth去验证
  42. web.ignoring().antMatchers("/app-server/api/**");
  43. }
  44.  
  45. @Override
  46. protected void configure(HttpSecurity http) throws Exception {
  47. http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
  48. http.authorizeRequests().anyRequest().fullyAuthenticated().antMatchers("/app-server/pc/**").hasRole("PCSERVER");
  49. // .antMatchers("/app-server/api/**").hasRole("APIUSER");
  50. http.csrf().disable();
  51. http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
  52. }
  53.  
  54. @Override
  55. protected void configure(AuthenticationManagerBuilder auth)throws Exception{
  56. auth.inMemoryAuthentication().withUser(admin_name).password(admin_password).roles(admin_roles)
  57. .and().withUser(pc_name).password(pc_password).roles(pc_roles)//PC 服务
  58. .and().withUser(app_name).password(app_password).roles(app_roles)//APP 服务
  59. .and().withUser(zuul_name).password(zuul_password).roles(zuul_roles) //路由
  60. .and().withUser(apiuser_name).password(apiuser_password).roles(apiuser_roles);//接口调用者
  61. }
  62.  
  63. }

application.yml

  1. server:
  2. port: 8000
  3. max-threads: 2000
  4. max-connections: 2000
  5.  
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. appname: eureka
  10. client:
  11. register-with-eureka: false
  12. fetch-registry: false
  13. service-url:
  14. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  15.  
  16. users:
  17. admin:
  18. name: admin
  19. password: admin
  20. roles: ADMIN,PC,APIUSER
  21. pc:
  22. name: pc
  23. password: 123456
  24. roles: PCSERVER
  25. app:
  26. name: app
  27. password: 123456
  28. roles: app
  29. apiuser:
  30. name: apiuser
  31. password: 123456
  32. roles: APIUSER
  33. zuul:
  34. name: zuul
  35. password: 123456
  36. roles: ZUUL

其他服务连接eureka

  1. http://pc:123456@localhost:8000/eureka/

使用SpringSecurity保护你的Eureka.的更多相关文章

  1. 使用SpringSecurity保护程序安全

    首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  2. 使用SpringSecurity保护方法应用

    (1)pom添加依赖 <dependency> <groupId>org.springframework.security</groupId> <artifa ...

  3. 笔记:Spring Cloud Eureka 常用配置及说明

    配置参数 默认值 说明 服务注册中心配置    Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean ...

  4. Eureka服务配置与进阶

    1. Eureka服务配置与进阶 1.1. 主要配置 1.1.1. 服务端(eureka.server.*) enableSelfPreservation默认true,启用注册中心的自保护机制,Eur ...

  5. Spring Cloud Eureka基本概述

    记一次Eureka的进一步学习. 一.Eureka简介 百科描述:Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡 ...

  6. Spring Cloud Eureka 常用配置及说明

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  7. spring cloud Eureka常见问题总结

    Spring Cloud中,Eureka常见问题总结. 指定Eureka的Environment 1 eureka.environment: 指定环境 参考文档:https://github.com/ ...

  8. Spring Cloud Eureka 属性作用

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  9. Spring Cloud(Dalston.SR5)--Eureka 常用配置

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

随机推荐

  1. git 安装使用

    系统ubuntu14.04 1.安装:sudo  apt-get install git 2.设置name and Email: git config --global user.name " ...

  2. VisionPro笔记(1):动态创建控件

     VisionPro学习笔记(1):动态创建控件 有的时候可能需要在程序中动态创建控件,VisionPro实例中提供了一例动态创建Blob控件的方法.当然,动态创建过多的控件会极大的消耗系统的资源,建 ...

  3. Ipython的安装/ipython notebook的简单使用

    安装了pyhton的windows版的shell好久了,今天才听说Ipython这个东西,确实在windows下的python shell很难用,一旦输入错误就要重新来过 了解了下ipython,然后 ...

  4. 【转】centos安装vim7.4

    centos安装vim7.4   系统版本centos6.4; root权限 su - root     卸载 $ rpm -qa | grep vim $ yum remove vim vim-en ...

  5. [UWP]新控件ColorPicker

    1. 前言 Fall Creators Update中提供了一个新得ColorPicker控件,解决了以前选择颜色只能用Combo Box的窘境. 2. 一个简单的例子 <ColorPicker ...

  6. js实现键盘按键检测

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  7. appium问题整理

    在刚进入appium的世界时,遇到无数的坑,趟过无数的浑水,现在整理一些常用的报错讯息,供大家参考 1.org.openqa.selenium.remote.UnreachableBrowserExc ...

  8. 不干胶打印机 www.bgjdyj.com

    不干胶打印机如何保养 不干胶打印机专卖网根据多年的维修经验总结了以下几种保养不干胶打印机的方法: 1.不干胶打印机打印机标签纸不能搁置太长时间,第一容易起静电.第二容易起灰尘2.不干胶打印机的打印头最 ...

  9. Spring源码情操陶冶-自定义节点的解析

    本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...

  10. 关于WebAPI

    1. 现在越来越多的企业以及网站 以及互联网使用WebApi  .那么WebApi 和 普通的WebServices  和WCF 最大的区别是什么了.那就是Web API很多人都会想到Web服务,但是 ...