springboot

运行原理

什么是springboot?

SpringBoot是一个快速开发框架,快速的将一些常用的第三方依赖整合(原理:通过Maven子父工程的方式),简化XML配置,全部采用注解形式,内置Http服务器(Jetty和Tomcat),最终以java应用程序进行执行。

  1. 首先是通过pom.xml配置父依赖,这也使我们导入依赖时不需要写版本,但是如果导入的包没有在依赖中管理着就需要手动配置版本。
  2. 默认的主启动器

//@SpringBootApplication 来标注一个主程序类
//说明这是一个Spring Boot应用
@SpringBootApplication
public class SpringbootApplication {

   public static void main(String[] args) {
     //以为是启动了一个方法,没想到启动了一个服务
      SpringApplication.run(SpringbootApplication.class, args);
   }

}
  • @SpringBootApplication
    这个注释的作用是标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动

在这个注解里面还有其他的注解:

例如

  • @ComponentScan:这个注解对应XML配置中的元素
    作用是:自动扫描并加载符合条件的组件或者bean,将这个bean加载到IOC容器中

  • @SpringBootConfiguration
    作用是:SpringBoot的配置类,标注在某个表上,表示这是一个SpringBoot的配置类

  • @Configuration:说明这是一个配置类,配置类就是对应Spring的xml配置文件

  • @Componennt:说明这是一个组件

  • @EnableAutoConfiguration:开启自动配置功能
    作用是:以前需要我们自己配置的东西,SpringBoot可以自动帮我们配置

@EnableAutoConfiguration这个注解里面提供了一个接口 AutoConfiguration

@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
  • @Import:Spring底层的注解@import,给容器中导入一个组件。Registrar.class作用:将主启动类的所在包和子包里面的所有组件扫描到Spring容器中

其实,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件并将其中对应的org.springframework.boot.autoconfigure.包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。

总结:1.SpringBoot启动时获取EnableAutoConfiguration指定的值
2.将这些值导入容器当中,自动配置类就生效
3.免去我们手动编写配置注入功能组件等操作

Thymeleaf

在SpringBoot中我们无法使用jsp,可以使用Thymeleaf模板引擎

html页面放置在类路径下的templates

下面是一些Thymeleaf的语法使用

Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
  Message Expressions: #{...}:获取国际化内容
  Link URL Expressions: @{...}:定义URL;
  Fragment Expressions: ~{...}:片段引用表达式
  
  Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,Number literals: 0 , 34 , 3.0 , 12.3 ,Boolean literals: true , false
      Null literal: null
      Literal tokens: one , sometext , main ,Text operations:(文本操作)
    String concatenation: +
    Literal substitutions: |The name is ${name}|
    
Arithmetic operations:(数学运算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
    
Boolean operations:(布尔运算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
    
Comparisons and equality:(比较运算)
    Comparators: > , < , >= , <=
    Equality operators: == , !=
    
Conditional operators:条件运算(三元运算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
    
Special tokens:
    No-Operation: _

mvc自动配置原理

SpringBoot自动配置了ViewResolver,就是之前学习的SpringMVC的视图解析器

可以自己去编写一个视图解析器


@Bean //放到bean中
public ViewResolver myViewResolver(){
    return new MyViewResolver();
}

//我们写一个静态内部类,视图解析器就需要实现ViewResolver接口
private static class MyViewResolver implements ViewResolver{
    @Override
    public View resolveViewName(String s, Locale locale) throws Exception {
        return null;
    }
}

SpringSecurity

Spring Security是一个框架,侧重于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求。
一般来说web应用的安全性包括用户认证和用户授权两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。
Spring Security 框架都有很好的支持。在用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。

我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!

  • WebSecurityConfigurerAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略

认证

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

授权

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

这个概念是通用的,而不是只在Spring Security 中存在。

@EnableWebSecurity // 开启WebSecurity模式
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");
  }
}

如果要对密码进行加密官方推荐使用bcrypt加密方式

权限控制和注销

//定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
   //....
   //开启自动配置的注销的功能
      // /logout 注销请求
   http.logout();
}

记住登录

//定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
   //记住
   http.rememberMe();
}

登录成功后,将cookie发送给浏览器保存,以后登录带上这个cookie,只要通过检查就可以免登录了,如果点击注销,则会删除这个cookie。

富文本编辑

  1. 首先先设计文章的数据库表
  2. 导入editor.md资源:https://pandao.github.io/editor.md/,还有很多可以进行选择。把资源放在examples目录下面
 $(function() {
       testEditor = editormd("article-content", {
           width : "95%",
           height : 400,
           syncScrolling : "single",
           path : "../editormd/lib/",
           saveHTMLToTextarea : true,    // 保存 HTML 到 Textarea
           emoji: true,
           theme: "dark",//工具栏主题
           previewTheme: "dark",//预览主题
           editorTheme: "pastel-on-dark",//编辑主题
           tex : true,                   // 开启科学公式TeX语言支持,默认关闭
           flowChart : true,             // 开启流程图支持,默认关闭
           sequenceDiagram : true,       // 开启时序/序列图支持,默认关闭,
           //图片上传
           imageUpload : true,
           imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
           imageUploadURL : "/article/file/upload",
           onload : function() {
               console.log('onload', this);
          },
           /*指定需要显示的功能按钮*/
           toolbarIcons : function() {
               return ["undo","redo","|",
                   "bold","del","italic","quote","ucwords","uppercase","lowercase","|",
                   "h1","h2","h3","h4","h5","h6","|",
                   "list-ul","list-ol","hr","|",
                   "link","reference-link","image","code","preformatted-text",
                   "code-block","table","datetime","emoji","html-entities","pagebreak","|",
                   "goto-line","watch","preview","fullscreen","clear","search","|",
                   "help","info","releaseIcon", "index"]
          },

           /*自定义功能按钮,下面我自定义了2个,一个是发布,一个是返回首页*/
           toolbarIconTexts : {
               releaseIcon : "<span bgcolor=\"gray\">发布</span>",
               index : "<span bgcolor=\"red\">返回首页</span>",
          },

           /*给自定义按钮指定回调函数*/
           toolbarHandlers:{
               releaseIcon : function(cm, icon, cursor, selection) {
                   //表单提交
                   mdEditorForm.method = "post";
                   mdEditorForm.action = "/article/addArticle";//提交至服务器的路径
                   mdEditorForm.submit();
              },
               index : function(){
                   window.location.href = '/';
              },
          }
      });
  });

异步,定时,邮件

异步任务

  1. 创建一个service包
  2. 创建一个ASyncService类
@Service
public class AsyncService {
@Async
   public void hello(){
       try {
           Thread.sleep(3000);
      } catch (InterruptedException e) {
           e.printStackTrace();
      }
       System.out.println("业务进行中....");
  }
}

@Async这个注解可以告诉Spring是一个异步方法
在使用这个注解时,我们需要先在主程序上添加一个注解@EnableAsync,开启异步注解功能

邮件任务

  1. 邮件发送引入spring-boot-start-mail
  2. SpringBoot自动配置MailSenderAutoConfiguration
  3. 定义MailProperties内容,配置在application.xml
  4. 自动装配javaMailSender
  5. 测试邮件发送
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值