vueblog实战


在这里插入图片描述

1.pom文件

<!--mysql + mybatis-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>
<!-- lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>

<!--spring web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!--热部署-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

2.application.yml

spring:
  datasource:
    url: jdbc:mysql://rm-bp18jitlw9a952i5x3o.mysql.rds.aliyuncs.com:3306/vueblog?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
server:
  port: 9200


#mybatis-config.xml
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

3. sql

 create table `m_user`(
    `id` BIGINT not null AUTO_INCREMENT ,

     `username` varchar(64) DEFAULT null,
     `password` varchar(64) DEFAULT null,
     `photourl` varchar(255) DEFAULT null,
     `email` varchar(64) default null,
     
     
     `status` int DEFAULT null,
     `created_time` datetime DEFAULT null,
     `last_login_time` datetime default null,
     primary KEY (`id`)

 )  ENGINE= INNODB DEFAULT charset= utf8;


 create table `m_blog`(
     `id` bigint not null AUTO_INCREMENT,
     `user_id` bigint not null,
     
     `title` varchar(255) not null,
     `description` varchar(512) not null,
     `content` LONGTEXT not null,
     
     `status` int DEFAULT null,
     `created_time` datetime DEFAULT null,
     PRIMARY KEY(`id`)
 ) ENGINE= INNODB DEFAULT charset= utf8;

4.Pojo

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private BigInteger id;
    private String username;
    private String password;
    private String photourl;
    private String email;
    
    private Integer status;
    private Timestamp createdTime;
    private Timestamp lastLoginTime;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Blog {
    private BigInteger id;
    private BigInteger userId;
    
    private String title;
    private String description;
    private String content;
    
    private Integer status;
    private Timestamp createdTime;
}

测试 UserDao UserService UserController

@Mapper
public interface UserDao {
    @Select("select * from m_user where id = 1")
    User getKcl();
}

@Service
public class UserService {
    @Autowired
    UserDao mUserDao;

    public User getKcl(){
        return mUserDao.getKcl();
    }
}

@Controller
public class UserController {
    @Autowired
    UserService mUserService;

    @RequestMapping("/kcl")
    @ResponseBody
    public User getKcl(){
        return mUserService.getKcl();
    }

}

或则mapper格式

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--mapper绑定interface接口-->
<mapper namespace="com.myblog.vueblog.dao.UserDao">
  
    
</mapper>

5.结果集封装

  • code:200表示成功, 400表示异常
  • msg: 结果消息
  • Object:结果数据
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {

    private int code; //200正常,其他表示异常
    private String msg;
    private Object data;

    // 静态方法直接调用,不需要new Object
    // 传递数据Obj,封装成Result对象
    // 附带code和msg
    public static Result success(int code,String msg,Object data){
        Result res = new Result(code,msg,data);
        return res;
    }
    // 成功重载
    public static Result success(Object data){
        return success(200,"操作成功",data);
    }
    
    
    // 失败
    public static Result failure(int code,String msg,Object data){
        Result r = new Result(code,msg,data);
        return r;
    }
    //失败重载 信息+数据
    public static Result failure(String msg,Object data){
        return failure(400,msg,data);
    }
    //失败重载  信息
    public static Result failure(String msg){
        return failure(400,msg,null);
    }
    
}

测试

    @RequestMapping("/kcl")
    @ResponseBody
    public Result getKcl(){
        return Result.success(200,"获取成功",mUserService.getKcl());
    }

6. 整合shiro+jwt,会话共享

shiro-redis链接

        <!--shiro-redis-->
        <dependency>
            <groupId>org.crazycake</groupId>
            <artifactId>shiro-redis-spring-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.3</version>
        </dependency>
        <!--jwt-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

ShiroConfig文件

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

@Configuration
public class ShiroConfig {

    /**
     * If you don’t have your own SessionManager or SessionsSecurityManager
     * in your configuration, shiro-redis-spring-boot-starter
     * will create RedisSessionDAO and RedisCacheManager for you.
     */
    @Autowired
    RedisSessionDAO redisSessionDAO;

    @Autowired
    RedisCacheManager redisCacheManager;


    //Inject them into your own SessionManager and SessionsSecurityManager
    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();

        // inject redisSessionDAO
        sessionManager.setSessionDAO(redisSessionDAO);

        // other stuff...

        return sessionManager;
    }

    @Bean
    public SessionsSecurityManager securityManager(AccountRealm realm, SessionManager sessionManager) {
        
        //创建安全管理器,自定义realm作为参数传递进去
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(realm);

        //inject sessionManager
        securityManager.setSessionManager(sessionManager);

        // inject redisCacheManager
        securityManager.setCacheManager(redisCacheManager);

        // other stuff...

        return securityManager;
    }
}

自定义realm, shiro.AccountRealm

@Component
public class AccountRealm extends AuthorizingRealm {
    
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值