spring 相关总结

 

一、 spring-boot-starter-data-jpa  spring-boot-starter-jdbc 区别

参考1: spring-boot-starter-data-jpa 与 spring-boot-starter-jdbc

spring-boot-starter-data-jpa 依赖 spring-boot-starter-jdbc, 说明 spring-boot-starter-jdbc 是相对底层的服务

 

二、 spring-data-jpa 理解

链接1: https://blog.csdn.net/luckyzsion/article/details/84569013  (Spring-data-jpa详解)

 链接2: https://baijiahao.baidu.com/s?id=1661937038552348304&wfr=spider&for=pc

 

三、 spring.factories理解 

Spring Boot 之spring.factories

 

四、  Field injection is not recommended

见:idea提示:Field injection is not recommended

五、@Configuration 和 @Component 区别

参考文档: 

1. @Configuration 和 @Component 到底有啥区别?

2.  源码解析@Configuration 和 @Component 的区别

 

六、 spring-security相关

自己的项目:

鉴权不通过返回结果:

{
    "code":1,
    "msg":"用户凭证已过期",
    "data":"Full authentication is required to access this resource"
}

源码位置:

ResourceAuthExceptionEntryPoint

 

@Override
	@SneakyThrows
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) {
		response.setCharacterEncoding(CommonConstants.UTF8);
		response.setContentType(ContentType.JSON.getValue());
		R<String> result = new R<>();
		result.setCode(CommonConstants.FAIL);
		response.setStatus(HttpStatus.UNAUTHORIZED.value());
		if (authException != null) {
			result.setMsg("error");
			result.setData(authException.getMessage());
		}

		// 针对令牌过期返回特殊的 424
		if (authException instanceof InvalidBearerTokenException
				|| authException instanceof InsufficientAuthenticationException) {
			response.setStatus(HttpStatus.FAILED_DEPENDENCY.value());
			result.setMsg(this.messageSource.getMessage("OAuth2ResourceOwnerBaseAuthenticationProvider.tokenExpired",
					null, LocaleContextHolder.getLocale()));
		}
		PrintWriter printWriter = response.getWriter();
		printWriter.append(objectMapper.writeValueAsString(result));
	}

  

 OAuth2ResourceOwnerBaseAuthenticationProvider.tokenExpired的多语言配置为:用户凭证已过期

AuthenticationException authException的相关代码:

org.springframework.security.web.access.ExceptionTranslationFilter#handleAccessDeniedException

new InsufficientAuthenticationException(
                            this.messages.getMessage("ExceptionTranslationFilter.insufficientAuthentication",
                                    "Full authentication is required to access this resource"))


authException.getMessage() 即为:Full authentication is required to access this resource。 

即可得到返回结果

七、 spring.factories 与 imports文件

参考:spring.factories将被弃用,做好Get新技能的准备了吗

 

八、下载静态文件

参考文档:https://blog.csdn.net/fuyingying_/article/details/107691999

Spring Boot 对静态资源映射提供了默认配置

Spring Boot 默认将 /** 所有访问映射到以下目录:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
Spring Boot 默认会挨个从 public resources static 里面找是否存在相应的资源,如果有则直接返回。
————————————————
原文链接:https://blog.csdn.net/fuyingying_/article/details/107691999

 1. 异常报错:404, NOT FOUND

解决方式:文件修改完之后, 在复制到代码中,不要代码中修改。

例如:原文件a.doc 需要替换成 a.docx

正确方式:将编辑好的a.docx直接放到工程中即可。

错误方式:b.docx 放到工程中,然后重命名成a.docx, 会报如上的错误

posted @ 2023-02-28 11:38  知行-zhixing  阅读(105)  评论(0编辑  收藏  举报