@Override @Transactional public void addLearningRecord(LearningRecordFormDTO dto) { //1.获得当前用户 Long user = UserContext.getUser(); //2.处理学习记录 boolean finished=false; //2.1判断提交的类型 if (dto.getSectionType()==SectionType.EXAM){ //处理考试 finished=handleExam(user,dto); }else { //处理视频 finished=handleVideo(user,dto); } }
时间: 2024-02-14 09:29:27 浏览: 39
这段代码是一个添加学习记录的方法。首先通过`UserContext.getUser()`获取当前用户的ID,并将其赋值给变量`user`。接下来根据传入的学习记录表单`dto`中的`sectionType`属性判断提交的类型。如果`sectionType`等于`SectionType.EXAM`,则表示提交的是考试类型的学习记录,会调用`handleExam(user,dto)`方法进行处理,并将返回结果赋值给变量`finished`。如果`sectionType`不等于`SectionType.EXAM`,则表示提交的是视频类型的学习记录,会调用`handleVideo(user,dto)`方法进行处理,并将返回结果赋值给变量`finished`。最后,方法执行完毕。
相关问题
public void updateView() { linearLayoutManager = new LinearLayoutManager(getContext()); binding.TagList.setLayoutManager(linearLayoutManager); Collections.sort(iv.inventoryList); InventoryViewAdapter adapter = new InventoryViewAdapter(iv.inventoryList); binding.TagList.setAdapter(adapter); constraintLayout = new ConstraintLayout(getContext()); adapter.setOnItemClickListener(new InventoryViewAdapter.RecyclerClink() { @Override public void onClink(View view, int position) { } @Override public void onLongClink(View view, int position) { // int a = position; // if (iv.inventoryList.get(position).status == 2) { // iv.inventoryList.get(position).status = 1; // } else { // iv.inventoryList.get(position).status = 2; // } // updateView(); } }); }什么意思
这段代码主要是用于更新界面视图,具体实现如下:
1. 创建一个线性布局管理器对象,并将其设置为RecyclerView的布局管理器;
2. 对数据进行排序;
3. 创建一个适配器对象,并将其设置为RecyclerView的适配器;
4. 将适配器对象设置为RecyclerView的适配器;
5. 创建一个约束布局对象;
6. 设置RecyclerView的长按事件监听器,在长按事件中更改数据状态,并调用updateView()方法更新视图。
修改一下这个inceptor,我要求查看请求访问”/user/api"的用户的role_id是否为2(通过token),否则拒绝访问:public class MyInceptor implements HandlerInterceptor { // 处理指定接口 private static final String TOKEN_LOGIN = "/user/token"; private static final String USER_NO_URL = "/user/no"; // 拦截不存在的接口路径 // 不要要校验的接口 private static final List<String> NO_NEED_LOGIN = new ArrayList<>(Arrays.asList("/user/health", "/logout")); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前执行的操作 System.out.println("=====preHandle====="); return true; // 返回true表示继续处理请求,返回false表示终止请求处理 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在请求处理之后,视图渲染之前执行的操作 System.out.println("=====postHandle====="); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在请求完成之后执行的操作,即视图渲染完成后 System.out.println("=====afterCompletion====="); } }
好的,您可以按照以下方式修改 MyInceptor 类的 preHandle 方法来实现您的要求:
```java
public class MyInceptor implements HandlerInterceptor {
// 处理指定接口
private static final String TOKEN_LOGIN = "/user/token";
private static final String USER_NO_URL = "/user/no"; // 拦截不存在的接口路径
// 不需要校验的接口
private static final List<String> NO_NEED_LOGIN = new ArrayList<>(Arrays.asList("/user/health", "/logout"));
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 在请求处理之前执行的操作
System.out.println("=====preHandle=====");
// 获取请求的路径
String requestURI = request.getRequestURI();
// 如果请求的路径是 /user/api,则需要进行校验
if ("/user/api".equals(requestURI)) {
// 获取请求头中的 token
String token = request.getHeader("token");
// 判断 token 是否有效,如果无效则拒绝访问
if (!isValidToken(token)) {
response.sendError(401, "Unauthorized");
return false;
}
// 获取 token 中的 role_id
int roleId = getRoleIdFromToken(token);
// 判断 role_id 是否为 2,如果不是则拒绝访问
if (roleId != 2) {
response.sendError(403, "Forbidden");
return false;
}
}
return true; // 返回true表示继续处理请求,返回false表示终止请求处理
}
private boolean isValidToken(String token) {
// TODO: 判断 token 是否有效
return true;
}
private int getRoleIdFromToken(String token) {
// TODO: 从 token 中获取 role_id
return 0;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// 在请求处理之后,视图渲染之前执行的操作
System.out.println("=====postHandle=====");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
// 在请求完成之后执行的操作,即视图渲染完成后
System.out.println("=====afterCompletion=====");
}
}
```
在 preHandle 方法中,首先判断请求的路径是否为 /user/api。如果是,则获取请求头中的 token,并判断其是否有效。如果 token 无效,则返回 401 错误(未授权)。如果 token 有效,则从中获取 role_id,并判断其是否为 2。如果 role_id 不为 2,则返回 403 错误(禁止访问)。如果请求的路径不是 /user/api,则直接返回 true,继续处理请求。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)