【项目一笔记】

GITEE第三方登录

1.gitee准备,在gitee页面,拉到最底下,点击openAPI,然后点击查看oauth文档,学习学习。然后在gitee 设置,在左边菜单栏找到第三方应用,随后创建应用。
编辑 应用名称,应用主页(),应用回调地址(http://localhost:8080/callback),权限(user_info)
2.代码,在登陆页面,给a标签【登陆】添加链接th:href="@{https://gitee.com/oauth/authorize(client_id='c liendID在gitee获取',redirect_uri=${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort()+'/callback'},response_type='code',scope='user_info',state=1)}"
其中的redirect_uri是thymeleaf写法,解释为获取项目的域名和端口。如果是本机上开发和测试也可以写成localhost:8080/callback。然后根据gitee传回的参数,在controller写一个/callback路由的方法。里面内容为获取gitee传过来的用户信息,然后封装,保存到数据库的user表里。

generator自动生成model与mapper

拦截器(拦截未登录用户)

1.新建类WebConfig(@Configuration)实现WebMvcConfigurer,然后重写addInterceptors方法,然后往里添加一个拦截器sessionInterceptor(自定义的)

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private SessionInterceptor sessionInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sessionInterceptor)
                .addPathPatterns("/**");
    }
}

2.新建类SessionInterceptor(@service)实现HandlerInterceptor的三个方法,
boolean preHandle里编写,内容为,从request里获取cookie,从cookie里获取token,然后根据该token去数据库查user,将user放到session里

几种stream流写法

//获取去重的评论人IDs
        Set<Long> commentators = comments.stream().map(comment -> comment.getCommentator()).collect(Collectors.toSet());
        List<Long> ids = new ArrayList<>();
        ids.addAll(commentators);
        //获取评论人
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andIdIn(ids);
        List<User> users = userMapper.selectByExample(userExample);
        Map<Long, User> userMap = users.stream().collect(Collectors.toMap(user -> user.getId(), user -> user));
        //转换cmment为DTO
        List<CommentDTO> commentDTOS = comments.stream().map(comment -> {
            CommentDTO commentDTO = new CommentDTO();
            BeanUtils.copyProperties(comment, commentDTO);
            commentDTO.setUser(userMap.get(comment.getCommentator()));
            return commentDTO;
        }).collect(Collectors.toList());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值