11、springboot

springboot

1、入门

1.springboot优点:
	(1)为所有spring开发者更快的入门。
	(2)开箱即用,提供各种默认配置来简化项目配置。
	(3)内嵌式容器简化web项目。
	(4)没有冗余代码生成和XML配置的要求。
2.微服务:是一种架构风格,它要求我们在开发一个应用的时候,这个应用必须构建成一系列小服务的组合,可以通过http的方式进行互通。
3.单体应用架构(all in one):我们将一个应用的中的所有应用服务都封装在一个应用中。
4.微服务的好处:
	(1)节省了调用资源
	(2)每个功能元素的服务都是一个可替换的、可独立升级的软件代码。
5.springboot简介:
	(1)简化spring应用开发的一个框架。
	(2)整个spring技术栈的一个大整合。
	(3)J2EE开发的一站式解决方案。
6.resources文件夹中目录结构:
	(1)static:保存所有的静态资源。
	(2)templates:保存所有的模板页面。
	(3)application.properties:配置文件。

2、配置文件

1.springboot使用一个全局的配置文件,文件名固定为:
	application.properties		application.yml
2.基本语法:
	(1)k:(空格)v:表示一对键值对(空格必须有)。
	(2)以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的。
3.@Value获取值和@ConfigurationProperties获取值比较:
@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散绑定支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持
    (1)功能:cp只需要写一次即可,value则需要每个字段都添加。
    (2)松散绑定:yml中写的last-name和lastName是一样的,-后面跟着的字母默认是大写的。
    (3)SpEL:Spring表达式语言,是比JSP的EL更强大的一种表达式语言。
    (4)JSR303数据校验:可以在字段增加一层过滤器验证,可以保证数据的合法性。
    (5)复杂类型封装:yml中可以封装对象。使用@value就不支持。
4.配置文件占位符:
	(1)随机数:${random.value}、${random.int}、${random.int(10)}。
	(2)占位符获取之前配置的值,如果没有可以是用:指定默认值。
5.Profile
	(1)多Profile文件:在主配置文件编写的时候,文件名可以是application-{profile}.properties/yml,默认使用application.properties的配置。
	(2)yml支持多文档块方式
	(3)激活指定profile:在配置文件中指定spring.profiles.active=dev

4、web开发

1.引入thymeleaf:导入依赖
2.导入thymeleaf的名称空间:<html lang="en" xmlns:"http://www.thymeleaf.org">
3.语法规则:th:任意html属性;用来替换原生属性的值。
4.默认访问页面:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("login");               		registry.addViewController("/main.html").setViewName("dashboard");
}
5.开发期间模板引擎页面修改后,实时生效:
	(1)禁用模板引擎的缓存:spring.thymeleaf.cache=false
	(2)页面修改后按Ctrl+f9;重新编译。
6.拦截器进行登录检查:
//注册拦截器
 @Override
public void addInterceptors(InterceptorRegistry registry) {
//静态资源:*.css , *.js
//springboot已经做好了静态资源映射
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/main.html");
            }
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user ==null){
            //未登录,返回登录页面
            request.setAttribute("msg","没有权限登录");
            request.getRequestDispatcher("/").forward(request,response);
            return false;
        }else{
            //已登录
            return true;
        }

    }
7.thymeleaf公共页面元素抽取:
	(1)抽取公共片段:<div th:fragment="copy">片段</div>
	(2)引入公共片段:<div th:insert="~{footer::copy}"></div>
	(3)默认效果:
		1)insert的公共片段中div标签中
		2)使用th:insert等属性进行引入,可以不用写~{}
		3)行内写法可以加上:[[~{}]];[(~{})]
	(4)三种引入公共片段的th属性:
		1)th:insert:将公共片段整个插入到声明引入的元素中。
		2)th:replace:将声明引入的元素替换为公
		共片段。
		3)th:include:将被引入的片段的内容包含进这个标签。
	(5)引入片段时传入参数:
<!--引入侧边栏;传入参数-->
<div th:replace="commons/bar::#sidebar(activeUri='emps.html')"></div>
    
<a class="nav-link active" href="#" th:href="@{/emps}"th:class="${activeUri=='emps.html'?'nav-link active':'nav-link'}">

5、数据访问

1.导入jdbc和mysql的依赖
2.整合mybatis(导入mybatis的依赖)
3.配置文件配置jdbc和mybatis:
spring:
	datasource:
	username:root
    password:123456
    url:jdbc:mysql://192.168.15.22:3306/jdbc
	driver--class-name:com.mysql.jdbc.Driver

6、自定义starter


7、用户认证和授权Security

1.简介:Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的web安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量配置,即可实现强大的安全管理。
2.Security相关类:
	(1)WebSecurityConfigurerAdapter:自定义Security策略。
	(2)AuthenticationManagerBuilder:自定义认证策略。
	(3)@EnableWebSecurity:开启WebSecurity模式
3.二个主要目标是“认证”和“授权”(访问控制)。
	“认证”(Authentication)
	“授权”(Authortization)
4.导入依赖security、thymeleaf、security-thymeleaf整合包:
<!--security-thymeleaf整合包-->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
5.创建配置类:
//AOP
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,功能页只有对应有权限的人才能访问
        //请求授权的规则
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限默认会到登录页面
            //定制登录页
        http.formLogin().loginPage("/toLogin");

        //防止网站攻击: get,post
        http.csrf().disable(); //关闭csrf功能

        //注销,开启了注销功能,跳到首页
        http.logout().logoutSuccessUrl("/");

        //开启记住我功能 cookie,默认保存2周  自定义接收前端的数据
        http.rememberMe().rememberMeParameter("remember");


    }

    //认证,springboot 2.1.x 可以直接使用
    //密码编码:PasswordEncoder
    //在Spring Security 5.0+ 新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该从数据库中读

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("yinwei").password(new BCryptPasswordEncoder().encode("1111")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("1111")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("1111")).roles("vip1");
    }
}

8、Shiro


9、任务


10、分布式 Dubbo + Zookeeper + SpringBoot

1.定义:分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统。分布式系统是由一组通过网络进行通信、为了完成共的任务而协调工作的计算机节点组成的系统。
2.目的:利用更多的机器,处理更多的数据。
3.RPC:远程过程调用,是一种进程间通信方式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值