SpringBoot项目需掌握的内容扩展

一 SpringBoot静态资源访问路径:

(1)静态资源默认访问路径:

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

(2)自定义静态资源路径

spring.resources.static-locations=classpath:/coding/,classpath:/glp/

一般:
css,js等放在static文件夹下
html 放在templates文件夹下

(3)网站主页:
静态资源文件夹下的所有index.html 页面;被/** 映射。
比如我访问http://localhost:8080/ ,就会找静态资源文件夹下的index.html

(4)网站图标:
Spring Boot在配置的静态内容位置中查找favicon.ico。如果存在这样的文件,它将自动用作应用程序的favicon。

#关闭默认图标
spring.mvc.favicon.enabled=false
二 模板引擎Thymeleaf

SpringBoot默认不支持默认是不支持jsp,而Thymeleaf和JSP的思想都是一样的,都是定义模板导入数据,然后生成页面,只不过语法不一样。

需要导入Thymeleaf依赖

官方文档:https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

thymeleaf的默认前缀是classpath:/templates/,所以我们只需要把html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可。

(1)编写一个TestController

@RequestMapping("/t1")
public String test1(Model model){
   //存入数据
   model.addAttribute("msg","Hello,Thymeleaf");
   //classpath:/templates/test.html
   return "test";
}

(2)编写一个测试页面test.html 放在templates 目录下
使用thymeleaf,需要在html文件中导入命名空间的约束xmlns:th="http://www.thymeleaf.org"

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>狂神说</title>
</head>
<body>
<h1>测试页面</h1>
<!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue一样-->
<div th:text="${msg}"></div>
</body>
</html>

thymeleaf的语法学习https://www.thymeleaf.org/
使用任意的th:attr 来替换Html中原生属性的值

页面存在缓存,所以我们需要禁用模板引擎的缓存

#禁用模板缓存
spring.thymeleaf.cache=false
三 SpringMVC的扩展配置

(1)编写SpringMVC配置类,实现WebMvcConfigurer接口。

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

  //自己定义一个视图类
   @Override
   public void addViewControllers(ViewControllerRegistry registry) {
       // 浏览器发送/test ,就会跳转到test页面;
       registry.addViewController("/test").setViewName("test");
  }
}

(2)SpringMVC的基本配置
SpringMVC使用@RequestBody和@ResponseBody,序列化/反序列化json数据(请求/响应json数据处理)的配置

# 美化JSON数据格式
spring.jackson.serialization.indent-output=true
# 设置JSON数据的日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# JSON数据属性为null时不返回
spring.jackson.default-property-inclusion=non_null

(3)SpringMVC使用@RequestParam时(请求参数)的日期格式
默认为yyyy/MM/dd

spring.mvc.date-format=yyyy-MM-dd HH:mm:ss

应用上下文路径,项目的根路径

server.servlet.context-path = /lucky

(4)登录拦截器

1)自定义拦截器:

public class LoginHandlerInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
       // 获取loginUser 信息进行判断
       Object user = request.getSession().getAttribute("loginUser");
       if (user == null){ // 未登录,返回登录页面
           request.setAttribute("msg","没有权限,请先登录");
         
 request.getRequestDispatcher("/index.html").forward(request,response);
           return false;
      }else {
           // 登录,放行
           return true;
      }
  }
}

2)将拦截器注册到我们的SpringMVC配置类当中

@Override
public void addInterceptors(InterceptorRegistry registry) {
   // 注册拦截器,及拦截请求和要剔除哪些请求!
   // 我们还需要过滤静态资源文件,否则样式显示不出来
   registry.addInterceptor(new LoginHandlerInterceptor())
      .addPathPatterns("/**")
      .excludePathPatterns("/index.html","/","/user/login","/asserts/**");
}
四 使用Restful风格实现我们的crud操作

在这里插入图片描述

五 集成Swagger

可以自动生成接口文档和客户端服务端代码,做到调用端代码、服务端代码以及接口文档的一致性。在项目中引入Springfox,可以扫描相关的代码,生成该描述文件,进而生成与代码一致的接口文档和客户端代码。这种通过代码生成接口文档的形式,在后面需求持续迭代的项目中,显得尤为重要和高效。

六 异步任务
七 定时任务
八 邮件发送
九 解决安全问题:Shiro,SpringSecurity

Spring Security(权限框架)是一个功能强大且高度可定制的身份验证和访问控制框架。一般来说,Web 应用的安全性包括 用户认证(Authentication)和用户授权(Authorization) 两个部分。

  • 用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。
  • 用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。

Apache Shiro 是一个Java 的安全(权限)框架。
Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。Shiro可以完成,认证,授权,加密,会话管理,Web集成,缓存等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值