SpringBoot+thymeleaf配置SSL证书(java代码配置)

我的网站 :flyhappypig

下载SSL证书

下载tomcat版本的SSL证书!!!

下载tomcat版本的SSL证书!!!

下载tomcat版本的SSL证书!!!

将下载好的证书放入resource目录下!!!

将下载好的证书放入resource目录下!!!

将下载好的证书放入resource目录下!!!

yml文件配置

# ssl配置
# https加密端口号 443
# 服务器运行端口
server:
  port: 443
  # SSL证书路径 一定要加上classpath,证书名只能有一个后缀,否则找不到不到文件,如:name.com.jks 则找不到
  ssl:
    key-store: classpath:xxx.jks
    # SSL证书密码
    key-store-password: xxxxx
    enabled: true
    # 证书类型
    key-store-type: JKS
    # 证书别名,建议设置为证书域名,否则可能报错(Invalid keystore format),如:www.baidu.com
    key-alias: xxx.com

# 设置:映射到443的端口
myPort: 9999

springboot启动类配置(注意是启动类配置)

// http 请求端口
    @Value("${myPort}")
    private int serverPortHttp;

    // 服务器运行端口,等同于 HTTPS 请求端口,线上 443
    @Value("${server.port}")
    private int serverPortHttps;
	 /**
     * http重定向到https(开发环境要注释)
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat;
        tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());

        return tomcat;
    }
    /**
	* 开发环境注释
	*/
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector(Http11NioProtocol.class.getName());
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(serverPortHttp);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(serverPortHttps);
        return connector;
    }

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的Spring Boot + Thymeleaf登录功能实现的示例。 1. 首先,我们需要在pom.xml文件中添加以下依赖项: ```xml <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> ``` 2. 在application.properties文件中添加如下配置: ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Thymeleaf配置 spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order=1 spring.thymeleaf.view-names=*.html ``` 3. 创建一个用户实体类User.java: ```java public class User { private String username; private String password; 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; } } ``` 4. 创建一个登录控制器LoginController.java: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String login(@ModelAttribute User user, Model model) { if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) { return "success"; } else { model.addAttribute("error", "用户名或密码错误"); return "login"; } } } ``` 5. 在src/main/resources/templates目录下创建一个名为login.html的登录页面: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>登录页面</h1> <form th:action="@{/login}" method="post"> <p> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> </p> <p> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </p> <p> <button type="submit">登录</button> </p> <p th:text="${error}" style="color:red"></p> </form> </body> </html> ``` 6. 在src/main/resources/templates目录下创建一个名为success.html的登录成功页面: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>登录成功</title> </head> <body> <h1>登录成功</h1> </body> </html> ``` 7. 启动应用程序,并访问http://localhost:8080/login进行登录。 以上就是一个简单的Spring Boot + Thymeleaf登录功能实现的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞天快乐猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值