SpringBoot集成和整合包资源

SpringBoot集成和整合包资源

  • 集成Mybatis
  • 集成Shiro
  • 集成Swagger2
  • 集成Redis
  • 集成Dubbo
  • 配置视图解析器
  • 发送邮件
  • 定时任务

集成Mybatis

依赖

<!--        druid-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.8</version>
</dependency>

<!--        mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

<!--        JDBC-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

在yml文件中配置

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.yyy.pojo

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/bjasd?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

集成Shiro

依赖

<!--        shiro-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.8.0</version>
</dependency>

ShiroConfig类

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);

        //添加shiro内置过滤器
        /*
        anno: 无需认证就能访问
        authc: 必须认证
        user: 必须 “记住我”功能
        perms: 拥有对某个资源的访问权限才能访问
        role: 拥有某个角色权限才能访问
        */
        Map<String, String> filterMap = new LinkedHashMap<>();

//        filterMap.put("/user/add", "authc");
//        filterMap.put("/user/update", "authc");
        filterMap.put("/user/add","perms[user:add]");
        filterMap.put("/user/update","perms[user:update]");
        
        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录请求
        bean.setLoginUrl("/login");
        //没有权限则跳转到的url
        bean.setUnauthorizedUrl("/unauthorized");

        return bean;
    }

    //DefaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager getSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //Realm 自定义的类
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }


}

UserRealm类

// 自定义Realm类 需要继承extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //System.out.println("============授权============");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取当前登录的对象
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        
        //添加权限
        info.addStringPermission(user.getPerms());
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //System.out.println("============认证============");
        //获取用户的token
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        
        //验证用户名是否存在数据库中
        User user = userService.getByUsername(token.getUsername());
        if (user == null) return null;
        
        //验密码是否正确
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}

集成Swagger2

依赖

<!--springfox启动器-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

SwaggerConfig类

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                //选择要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.yyy.controller"))
                //过滤不扫描的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("描述")
                .termsOfServiceUrl("https://yyy.com/")
                .contact(new Contact("yyy", "https://yyy.com","yyy@qq.com"))
                .version("1.0")
                .build();
    }
}

properties配置

# ================解决swagger3报错==========================
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
#=========================================================

集成Redis

依赖

<!--        redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

yml配置

spring:  
  redis:
    host: 127.0.0.1
    password: 123456
    port: 6379

集成Dubbo

接口工程
存放实体bean和业务接口

服务提供者

业务接口的实现类并将服务暴露且注册到注册中心,调用持久层
依赖

<!--        dubbo-->
<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<!--        注册中心-->
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

<!--        接口工程-->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>springboot-dubbo-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

配置文件

#dubbo配置
spring.application.name=provider
#当前工程为提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

在入口Application类上加注解@EnableDubboConfiguration,开启Dubbo配置。
编写接口实现类

@Component
//Service是Dubbo的注解!!!!!!
@Service(interfaceClass = StudentService.class,version = "1.0.0")
public class StudentServiceImpl implements StudentService {
    @Override
    public Integer getCount() {
        return 20;
    }
}

服务消费者

处理浏览器客户端发送来的请求,从注册中心调用服务提供者提供的服务
依赖

<!--        dubbo-->
<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<!--        注册中心-->
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

<!--        接口工程-->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>springboot-dubbo-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

配置文件

#dubbo配置
spring.application.name=consumer
#指定注册中心
spring.dubbo.registry=zookeeper://localhost:2181

在入口Application类上加注解@EnableDubboConfiguration,开启Dubbo配置。
在Controller中调用

@RestController
public class StudentController {
    //将注册中心的接口实现类注入进去
    //Reference是Dubbo的!!!!!
    @Reference(interfaceClass = StudentService.class,version = "1.0.0")
    StudentService studentService;
    
    @RequestMapping("/student/count")
    public Object count(){
        Integer count = studentService.getCount();
        return "总人数为:" + count;
    }
}

最后,先启动zookeeper服务器,在启动provider服务提供者,最后启动consumer服务消费者,浏览器输入地址即可。


配置视图解析器

自建类

@Configuration
public class MyViewResolver implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/charts").setViewName("charts");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/").setViewName("login");
    }
}

发送邮件

依赖

<!--        mail-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

properties配置

spring.mail.username=邮箱账户
spring.mail.password=加密后的密码bsycuksdugrubcjg
spring.mail.host=smtp.qq.com
#qq需要开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

自动注入

@Autowired
JavaMailSenderImpl sender;

简单的邮件

SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("标题!");
message.setText("内容!");
message.setFrom("发送者邮箱");
message.setTo("接收者邮箱");
sender.send(message);

复杂的邮件

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("标题!");
helper.setText("<h1 style='color:red'>内容!</h1>",true);
helper.setFrom("发送者邮箱");
helper.setTo("接收者邮箱");
//附件
helper.addAttachment("文件名",new File(""));
sender.send(message);

定时任务

在主类上添加@EnableScheduling注解,方法上添加@Scheduled(cron = "")注解
cron表达式

cron = "秒 分 时 日 月 周几"
* 表示所有
? 表示不确定
例:
"0 0 10,14,16 * * ?" 每天上午10点,下午2点,4点
"0 0/30 9-17 * * ?" 朝九晚五工作时间内每半小时
"0 0 12 ? * WED" 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

posted @ 2022-03-03 15:03  YYYangFFF  阅读(141)  评论(0)    收藏  举报