Spring Boot 使用SSL-HTTPS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 使用SSL-HTTPS相关的知识,希望对你有一定的参考价值。

参考技术A 我们公司现在使用springcloud做的微服务架构项目,默认消息流都是HTTP通道,引入HTTPS通道整体的消息流大概为:

Spring Boot中使用HTTPS步骤

私匙,公钥、 格式区别:

证书生成和配置:

(1)要有一个SSL证书,证书怎么获取呢?买(通过证书授权机构购买)或者自己生成(通过keytool生成)。

(2)在spring boot中启用HTTPS

开启https

程序内配置添加http

参考:

springcloud搭建HTTPS安全通道

Eureka Server启用 https服务指北

Spring boot同时支持http和https访问

Spring Boot 使用SSL-HTTPS

spring boot支持https请求

keystore导出p12,cer,crt,.key.pem证书文件格式

证书之间的转换(crt pem key)

SSL/TLS 双向认证(一) -- SSL/TLS工作原理

spring boot系列spring boot 使用mongodb

1 pom.xml配置

增加包依赖:spring-boot-starter-data-mongodb

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

2 application.properties配置文件中增加:

spring.data.mongodb.uri=mongodb://127.0.0.1:27017/ceshidb

ceshidb是数据库名字

    2.4版本以上的mongodb采用如下格式配置:

spring.data.mongodb.uri=mongodb://root(userName):root(password)@localhost(ip地址):27017(端口号)/gis数据库)

 3 创建实体类

package com.cfj.ceshi.entity;


public class UserEntity {
    
    private static final long serialVersionUID = 1L;
    private Long id;
    private String userName;
    private String passWord;
    

    public UserEntity() {
        super();
    }

    public UserEntity(String userName, String passWord) {
        super();
        this.passWord = passWord;
        this.userName = userName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    
    @Override
    public String toString() {
        return "userName " + this.userName + ", pasword " + this.passWord;
    }

}

4 创建dao接口和实现类

package com.cfj.ceshi.dao;

import com.cfj.ceshi.entity.UserEntity;

public interface UserDao {
    
    public void saveUser(UserEntity user);

    public UserEntity findUserByUserName(String userName);

    public int updateUser(UserEntity user);

    public void deleteUserById(Long id);

}
package com.cfj.ceshi.dao.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.cfj.ceshi.dao.UserDao;
import com.cfj.ceshi.entity.UserEntity;
import com.mongodb.WriteResult;

@Repository
public class UserDaoImpl implements UserDao{
    
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveUser(UserEntity user) {
        //mongoTemplate.save(user);
        mongoTemplate.save(user, "jihe01");//指定集合
        
    }

    @Override
    public UserEntity findUserByUserName(String userName) {
        Query query = new Query(Criteria.where("userName").is(userName));
        //UserEntity user =  mongoTemplate.findOne(query, UserEntity.class);
        UserEntity user =  mongoTemplate.findOne(query, UserEntity.class, "jihe01");//指定集合
        return user;
    }

    @Override
    public int updateUser(UserEntity user) {
        Query query=new Query(Criteria.where("id").is(user.getId()));
        Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
        //更新查询返回结果集的第一条
        WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class, "jihe01");
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,UserEntity.class);
        if(result!=null)
            return result.getN();
        else
            return 0;
    }

    @Override
    public void deleteUserById(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,UserEntity.class, "jihe01");
        
    }
    
    

}

 5 创建service接口和实现类

package com.cfj.ceshi.service;

import com.cfj.ceshi.entity.UserEntity;

public interface UserService {
    
    public void saveUser(UserEntity user);

    public UserEntity findUserByUserName(String userName);

    public int updateUser(UserEntity user);

    public void deleteUserById(Long id);
   
}
package com.cfj.ceshi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cfj.ceshi.dao.UserDao;
import com.cfj.ceshi.entity.UserEntity;
import com.cfj.ceshi.service.UserService;


@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
    private UserDao userDao;

    @Override
    public void saveUser(UserEntity user) {
        userDao.saveUser(user);
    }

    @Override
    public UserEntity findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }

    @Override
    public int updateUser(UserEntity user) {
        return userDao.updateUser(user);
    }

    @Override
    public void deleteUserById(Long id) {
        userDao.deleteUserById(id);
    }
    

}

6 创建web(control)层

package com.cfj.ceshi.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cfj.ceshi.entity.UserEntity;
import com.cfj.ceshi.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    
    //按名字查询
    @RequestMapping("/getUserByName")
    public String getUserByName(String name) {
        UserEntity user= userService.findUserByUserName(name);
        return user.toString();
    }
    
    //增加方法
    @RequestMapping("/add")
    public void save() {
         UserEntity user=new UserEntity();
         user.setId(3l);
         user.setUserName("小明");
         user.setPassWord("fffooo123");
         userService.saveUser(user);
     }
     //更新方法
     @RequestMapping("/update")
     public void update(UserEntity user) {
         userService.updateUser(user);
     }
     //删除方法
     @RequestMapping(value="/delete")
     public void delete(Long id) {
         userService.deleteUserById(id);
     }
    

}

 7 可以使用postman等工具进行测试

 

代码地址:https://gitee.com/kaixinmao/springboot-mongodb

以上是关于Spring Boot 使用SSL-HTTPS的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 使用SSL-HTTPS

使用 spring-boot:run 时是不是可以使用 spring-boot 命令行属性?

spring boot系列spring boot 使用mongodb

spring boot 2.0之使用spring boot

spring boot8.spring boot的日志框架使用

Spring Boot(十八):使用Spring Boot集成FastDFS