Java登录与注册实战

一、单点登录的三种常用方式
第一种:session广播机制实现
第二种:使用cookie+redis实现
第三种:使用token实现
目前使用最多的是第二种,一般要考虑到某一个时间段用户注册的数量,比如做活动推广的时候;这时除了利用redis缓存,还需要加大带宽等等;
其次是使用token;目前这两种用的比较多。
postman在测试接口的时候也常常复制自己的登录信息测通接口,或者设置白名单机制。

第二种:使用cookie+redis实现
1、redis(key-value):key唯一值,value用户数据;
cookie:把redis生成key–》cookir
2、访问项目中其他模块,发送请求带着cookie进行发送,获取cookie值,拿着cookie做功能。

也就是把cookie获取值,到redis进行查询,根据key进行查询,如果查询到数据就是可以登录。

第三种情况:带token
1、按照一定规则生成字符串,可以包含用户信息(加密)
(1)可以把字符串通过cookie返回
(2)可以把字符串通过地址栏返回
2、再去访问项目其他模块,每次访问在地址栏带着生成字符串,在访问模块里面获取地址栏字符串,根据字符串获取用户信息。如果可以获取,则可以登录。

二、登录开发流程、思路
1、获取手机号和密码、验证码(现在有很多登录接口是不用密码,那么直接初始化的密码即可)
2、校验手机号和密码、验证码是否为空
3、校验手机号和密码、验证码是否符合格式(正则表达式)
4、判断该用户对象是否存在数据库(过滤条件下)
5、判断密码(解密)
6、判断用户是否禁用(可以在4直接过滤)
7、登录成功,生成token字符串,使用jwt工具类

三、注册开发流程、思路
1、获取手机号和密码、验证码(现在有很多登录接口是不用密码,那么直接初始化的密码即可)
2、校验手机号和密码、验证码是否为空
3、校验手机号和密码、验证码是否符合格式(正则表达式)
4、判断手机号是否存在数据库—》已经注册或账号不正常
5、获取到的信息数据插入数据库中

aop spring实现

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* 配置类
*/
//启动代理
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
@Bean
public WorkClass calculator() {
return new WorkClass();
}

@Bean
public LogAspects logAspects() {
return new LogAspects();
}
}
/**
* 业务类
*/
public class WorkClass {
public int test() {
System.out.println("====================hello");
return 1;
}
}
/**
* 切面类
*/
@Aspect
public class LogAspects {

@Pointcut("execution(public int com.report.server.util.WorkClass.*(..))")
public void pointCut() {};

@Before("pointCut()")
public void logStart(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println(" " + joinPoint.getSignature().getName() + "run........@Before:"
+ Arrays.asList() + "" + args);
}

@After("com.report.server.util.LogAspects.pointCut()")
public void logEnd(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName() + "....end @After");
}

@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
System.out.println(joinPoint.getSignature().getName() + "正常返回。。。@AfterReturning:reuslt:" + result);

}

@AfterThrowing(value = "pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception) {
System.out.println(joinPoint.getSignature().getName() + "异常新:" + exception);
}


}
/**
* 帮助工具类测试
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ReportApplication.class)
public class UtilClassTest {


@Test
public void testee() {
System.out.println("00000");
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
WorkClass bean = applicationContext.getBean(WorkClass.class);
bean.test();
applicationContext.close();

}
}

结果

00000
testrun........@Before:[][Ljava.lang.Object;@2e47ec5e
====================hello
test....end @After
test正常返回。。。@AfterReturning:reuslt:1