发表于: 2020-07-02 23:49:01

1 1719


今天完成


sprringSecurity搭建


依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
   <groupId>org.thymeleaf.extras</groupId>
   <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>




控制层

@Controller
public class RouterController {

       @GetMapping("/")
       public String index(){
           return "index";
       }
       @GetMapping("/boot/{id}")
       public String springbootById(@PathVariable int id){
           return "boot_"+id;
       }
       @GetMapping("/work/{id}")
       public String work(@PathVariable int id){
           return "work_"+id;
       }

   }


授权和认证

@EnableWebSecurity //注解开启Spring Security的功能。
public class SecurityConfig extends WebSecurityConfigurerAdapter {    //继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节。


   @Override
   protected void configure(HttpSecurity http) throws Exception {

       // 定制授权规则
       http.authorizeRequests().antMatchers("/").permitAll(). // 所有角色可访问
               antMatchers("/boot/**").hasAnyRole("admin", "test"). // 只有xx角色才能访问
               antMatchers("/work/**").hasRole("admin") // 只有xx角色才能访问
               .and()
               .formLogin()   //没有权限就默认跳转到登录页  "/login" (登录页)
               .and()
               .logout()  // 退出并清除session
               .logoutSuccessUrl("/") //注销成功跳转到  "/" (index.html)  页面
               .and()
               .rememberMe() // 开启记住我功能
               .rememberMeParameter("remember") //自定义rememberMename值,默认remember-Me
               .tokenValiditySeconds(10); // 记住时间
   }


   // 定义认证规则
   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
               .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("admin","test")
               .and().passwordEncoder(new BCryptPasswordEncoder())
               .withUser("test").password(new BCryptPasswordEncoder().encode("123456")).roles("test");
   }
}




账户密码有三种方式配置

1.内存(如图)

2.配置文件

3.数据库中


数据库中得还没整合  

等后面做项目用到时再把用户密码转换为数据库中的数据



springCloud


springCloud有一个Eureka

专门用于给其他服务注册的称为Eureka Server(服务注册中心)

其余注册到Eureka Server的服务称为Eureka Client。





Eureka Client分为服务提供者和服务消费者

  • 但很可能,某服务既是服务提供者又是服务消费者

  • 也可以是单纯的服务消费者:
  • 单纯的服务消费者可以无需"注册"到Eureka-Server,因为单纯的服务消费者无需对外提供服务,也就无须注册到Eureka中



Eureka的治理机制:



  • 1. 服务提供者

    • 服务注册:启动的时候会通过发送REST请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息。

    • **服务续约:**在注册完服务之后,服务提供者会维护一个心跳用来持续告诉Eureka Server: "我还活着 ” 、

    • 服务下线:当服务实例进行正常的关闭操作时,它会触发一个服务下线的REST请求给Eureka Server, 告诉服务注册中心:“我要下线了 ”。

  • 2. 服务消费者

    • 获取服务:当我们启动服务消费者的时候,它会发送一个REST请求给服务注册中心,来获取上面注册的服务清单
    • 服务调用:服务消费者在获取服务清单后,通过服务名可以获得具体提供服务的实例名和该实例的元数据信息。在进行服务调用的时候,优先访问同处一个Zone中的服务提供方


  • Eureka Server(服务注册中心):

    • 失效剔除:默认每隔一段时间(默认为60秒) 将当前清单中超时(默认为90秒)没有续约的服务剔除出去

    • 自我保护:。EurekaServer 在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%(通常由于网络不稳定导致)。 Eureka Server会将当前的实例注册信息保护起来, 让这些实例不会过期,尽可能保护这些注册信息



大概的调用流程图





Eureka的主页面




所有已经注册好的会显示名字,等待其他客户端调用








已经运行完毕的的模块



server里是Eureka Server(服务注册中心)


client是服务提供者


fegin和ribbon是服务消费者

可以用来配置负载均衡  均衡的访问不同的client



明天计划


学习其他的springcloud功能模块





返回列表 返回列表
评论

    分享到