发表于: 2018-10-19 23:11:48

1 411


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

添加了JWT:

package cn.zzh.foreground_client.project.tools.security;

/**
* @Author: 快乐水 青柠可乐
* @Description:
* @Date: Created in 上午10:41 2018/10/18
* @Modified By:
*/
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

public class JwtTool {
/**
    * APP登录Token的生成和解析
    *
    */

   /** token秘钥,请勿泄露,请勿随便修改 backups:JKKLJOoasdlfj */
   private static final String SECRET = "JKKLJOoasdlfj";
   /** token 过期时间: 10天 */
   private static final int CALENDAR_FIELD = Calendar.DATE;
   private static final int CALENDAR_INTERVAL = 10;

   /**
    * JWT生成Token.<br/>
    *
    * JWT构成: header, payload, signature
    *
    * @param user_id
    *            登录成功后用户user_id, 参数user_id不可传空
    */
   public static String createToken(Long user_id) throws Exception {
Date iatDate = new Date();
       // expire time
       Calendar nowTime = Calendar.getInstance();
       nowTime.add(CALENDAR_FIELD, CALENDAR_INTERVAL);
       Date expiresDate = nowTime.getTime();

       // header Map
       Map<String, Object> map = new HashMap<>();
       map.put("alg", "HS256");
       map.put("typ", "JWT");

       // build token
       // param backups {iss:Service, aud:APP}

       // header
       String token = JWT.create().withHeader(map)
// payload
               .withClaim("iss", "Service")
.withClaim("aud", "APP").withClaim("user_id", null == user_id ? null : user_id.toString())
// sign time
               .withIssuedAt(iatDate)
// expire time
               .withExpiresAt(expiresDate)
// signature
               .sign(Algorithm.HMAC256(SECRET));

       return token;
   }

/**
    * 解密Token
    *
    * @param token
    * @return
    * @throws Exception
    */
   public static Map<String, Claim> verifyToken(String token) {
DecodedJWT jwt = null;
       try {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
           jwt = verifier.verify(token);
       } catch (Exception e) {
e.printStackTrace();
           // token 校验失败, 抛出Token验证非法异常
       }
return jwt.getClaims();
   }

/**
    * 根据Token获取user_id
    *
    * @param token
    * @return user_id
    */
   public static Long getAppUID(String token) {
Map<String, Claim> claims = verifyToken(token);
       Claim user_id_claim = claims.get("user_id");
       if (null == user_id_claim || StringUtils.isEmpty(user_id_claim.asString())) {
// token 校验失败, 抛出Token验证非法异常
       }
return Long.valueOf(user_id_claim.asString());
   }
}

2: 优化了SQL,对select的值进行筛选:

package cn.zzh.foreground_client.project.dao;

import cn.zzh.foreground_client.project.entity.User;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

public interface UserMapper {
@Select({
"select count(*) from user where phone_number=#{phoneNumber}"
   })
int selectExitPhoneNumber(String phoneNumber);

   @Select({
"select phone_number from user",
           "where password = #{password}"
   })
String selectPhoneByPwd (String phoneNumber);

   @Select({
"select",
       "id, serial_id, phone_number, id_card, real_name, is_locaked, is_noob,",
       " balance, profit, created_at, updated_at",
       "from user",
       "where id = #{id,jdbcType=BIGINT}"
   })
@ResultMap("UserNormalInfo")
User selectByPrimaryKey(Long id);

   @Select({
"select count(*) from user ",
           "where id = #{id,jdbcType=BIGINT} and password=#{password,jdbcType=VARCHAR} "
   })
int selectExitByIdPwd(Long id,String pwd);

   @Select({
"select real_name from user where id=#{id,jdbcType=BIGINT}"
   })
@ResultMap("UserNormalInfo")
User selectForCompact(Long id);

   User selectSelective(User record);


   int insertSelective(User record);

   int updateByPrimaryKeySelective(User record);

   int updateByPhoneNumberSelective(User record);





}

3:查看是否缺少字段,给compact增加一个联合user,product的查询接口;

<resultMap id="JoinResultMap" type="cn.zzh.foreground_client.project.entity.Compact" >
   <id property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="serialId" column="serial_id" jdbcType="VARCHAR" javaType="java.lang.String" />
   <result property="userId" column="user_id" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="productId" column="product_id" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="expireTime" column="expire_time" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="status" column="status" jdbcType="TINYINT" javaType="java.lang.Byte" />
   <result property="isMatch" column="is_match" jdbcType="TINYINT" javaType="java.lang.Byte" />
   <result property="amount" column="amount" jdbcType="DECIMAL" javaType="java.math.BigDecimal" />
   <result property="profit" column="profit" jdbcType="DECIMAL" javaType="java.math.BigDecimal" />
   <result property="financialTime" column="financialTime" jdbcType="INTEGER" javaType="java.lang.Integer" />
   <result property="repaymentWay" column="repayment_way" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="createdAt" column="created_at" jdbcType="BIGINT" javaType="java.lang.Long" />
   <result property="updatedAt" column="updated_at" jdbcType="BIGINT" javaType="java.lang.Long" />
 <association property="product" column="product_id" javaType="cn.zzh.foreground_client.project.entity.Product"
              select="cn.zzh.foreground_client.project.dao.ProductMapper.selectForCompact"/>
   <association javaType="cn.zzh.foreground_client.project.entity.User"
                property="user" column="user_id"
                select="cn.zzh.foreground_client.project.dao.UserMapper.selectForCompact"/>

4:测试mybatis的结果集映射的几种情况:


mybatis会自动创建一个ResultMap对象,然后基于查找出来的属性名进行key-balue的封装;
接着根据返回类型的对象,从map中取出与对象对应的键值进行赋值
当返回类型直接是一个resultMap的时候,可以用在复杂联合上,也可以在字段不一定能对应的情况下创建一个resultMap;
当结果集和resultType或者resultMap中数据量对应偏少的时候;
结果集,resultMap和javaBean,虽然是3个东西;但是应该是两个东西:结果集,javaBean
(rasultMap只是作为一个javaBean的一个字段映射,防止columns和properties不能自动映射的情况)
然后,如果能自动映射,resultMap为空不影响映射结果;
然后结果集和javaBean在保证能一一映射和有空构造器以及get,Set方法的时候,
数据量是结果集多还是javaBean多,是不影响的,只会映射和匹配能匹配的;



明天计划的事情:(一定要写非常细致的内容) 

最后的测试,然后联条;
遇到的问题:(遇到什么困难,怎么解决的) 

sql的语句在mybatis使用left函数出问题了:

如图,在terminal是可以使用的;

但是写在mybatis的时候,是映射不成功的,一直是null值;


收获:(通过今天的学习,学到了什么知识)

如上


返回列表 返回列表
评论

    分享到