当前位置:   article > 正文

SpringCloud Security 安全认证_security.enable

security.enable

1、加入jar

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

2、resource下yml文件添加

  1. # 安全认证的配置
  2. security:
  3. basic:
  4. enabled: false #是否生成随机密码
  5. user:
  6. name: root
  7. password: root

3、security账户密码加密类

  1. @Configuration
  2. public class SecurityConfig {
  3. @Bean
  4. public HttpHeaders getHeaders(){
  5. HttpHeaders headers = new httpHeaders();
  6. String auth = "root:root";
  7. byte[] encoderAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
  8. //此处注意Basic需要跟账号密码空格隔开
  9. String authHeader = "Basic " + new String(encoderAuth);
  10. headers.set("Authorization",authHeader);
  11. return headers;
  12. }
  13. @Bean
  14. public RestTemplate getRestTemplate(){
  15. return new RestTemplate();
  16. }
  17. }

4、restController调用示例

  1. @RestController
  2. public class RestController {
  3. @Resource
  4. SecurityConfig securityConfig;
  5. @Resource
  6. HttpHeaders headers;
  7. @RequestMapping(value = "addDate",method = RequestMethod.POST)
  8. public Object addData(Dept dept){
  9. //exchange(urlm,请求方法,请求体参数(传入参数(可选),header体加密数据),映射对象)
  10. return securityConfig.getRestTemplate().exchange(url,HttpMethod.POST,new HttpEntity<Object>(dept,this.headers),Dept.class).getBody();
  11. }
  12. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/953477
推荐阅读
相关标签
  

闽ICP备14008679号