提交 114229d4 authored 作者: gongwenjie's avatar gongwenjie

合并分支 'gwj' 到 'master'

登录 查看合并请求 !11
流水线 #17 已失败 于阶段
...@@ -3,6 +3,7 @@ package com.zjty.tynotes.pas.config; ...@@ -3,6 +3,7 @@ package com.zjty.tynotes.pas.config;
import com.zjty.tynotes.pas.config.handler.MyAccessHandler; import com.zjty.tynotes.pas.config.handler.MyAccessHandler;
import com.zjty.tynotes.pas.config.handler.MyFailHandler; import com.zjty.tynotes.pas.config.handler.MyFailHandler;
import com.zjty.tynotes.pas.config.handler.MyLogoutHandler; import com.zjty.tynotes.pas.config.handler.MyLogoutHandler;
import com.zjty.tynotes.pas.config.handler.MySuccessHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -46,6 +47,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -46,6 +47,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
private MyFailHandler failHandler; private MyFailHandler failHandler;
@Autowired
private MySuccessHandler successHandler;
@Override @Override
public void configure(HttpSecurity httpSecurity) throws Exception { public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity httpSecurity
...@@ -90,6 +95,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -90,6 +95,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Bean
public MyFilter myFilter() throws Exception { public MyFilter myFilter() throws Exception {
MyFilter filter = new MyFilter(); MyFilter filter = new MyFilter();
filter.setAuthenticationSuccessHandler(successHandler);
filter.setAuthenticationFailureHandler(failHandler); filter.setAuthenticationFailureHandler(failHandler);
filter.setFilterProcessesUrl("/userLogin"); filter.setFilterProcessesUrl("/userLogin");
filter.setAuthenticationManager(this.authenticationManager()); filter.setAuthenticationManager(this.authenticationManager());
......
...@@ -3,10 +3,17 @@ package com.zjty.tynotes.pas.config.handler; ...@@ -3,10 +3,17 @@ package com.zjty.tynotes.pas.config.handler;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjty.tynotes.pas.entity.Authority; import com.zjty.tynotes.pas.entity.Authority;
import com.zjty.tynotes.pas.entity.Role; import com.zjty.tynotes.pas.entity.Role;
import com.zjty.tynotes.pas.dao.AuthorityDao;
import com.zjty.tynotes.pas.dao.RoleAuthorityDao;
import com.zjty.tynotes.pas.dao.RoleDao;
import com.zjty.tynotes.pas.dao.UserRoleDao;
import com.zjty.tynotes.pas.entity.*;
import com.zjty.tynotes.pas.entity.User; import com.zjty.tynotes.pas.entity.User;
import com.zjty.tynotes.pas.service.IAuthorityService; import com.zjty.tynotes.pas.service.IAuthorityService;
import com.zjty.tynotes.pas.service.IRoleService; import com.zjty.tynotes.pas.service.IRoleService;
import com.zjty.tynotes.pas.service.IUserService; import com.zjty.tynotes.pas.service.IUserService;
import com.zjty.tynotes.pas.task.Init;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -27,7 +34,9 @@ import javax.servlet.http.Cookie; ...@@ -27,7 +34,9 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* @author mcj * @author mcj
...@@ -49,6 +58,18 @@ public class MySuccessHandler implements AuthenticationSuccessHandler { ...@@ -49,6 +58,18 @@ public class MySuccessHandler implements AuthenticationSuccessHandler {
@Autowired @Autowired
IUserService iUserService; IUserService iUserService;
RoleDao roleDao;
@Autowired
private UserRoleDao userRoleDao;
@Autowired
private RoleAuthorityDao roleAuthorityDao;
@Autowired
private AuthorityDao authorityDao;
@Autowired
private Init init;
@Autowired @Autowired
RedisTemplate redisTemplate; RedisTemplate redisTemplate;
...@@ -74,13 +95,33 @@ public class MySuccessHandler implements AuthenticationSuccessHandler { ...@@ -74,13 +95,33 @@ public class MySuccessHandler implements AuthenticationSuccessHandler {
} }
} }
redisTemplate.opsForValue().set(user.getUsername(),0); // redisTemplate.opsForValue().set(user.getUsername(),0);
if(("root").equals(user.getUsername())){
user = init.root;
}else{
List<UserRole> userRoles = userRoleDao.findAllByUserId(user.getId());
List<Role> roles = new ArrayList<>();
for (UserRole userRole : userRoles) {
String roleId = userRole.getRoleId();
Optional<Role> op = roleDao.findById(userRole.getRoleId());
if(op.isPresent()){
Role role = op.get();
List<RoleAuthority> roleAuthorities = roleAuthorityDao.findAllByRoleId(roleId);
List<String> authorityIds = new ArrayList<>();
for (RoleAuthority roleAuthority : roleAuthorities) {
authorityIds.add(roleAuthority.getAuthorityId());
}
List<Authority> authorities = authorityDao.findAllByIdIn(authorityIds);
role.setAuthorities(authorities);
roles.add(role);
}
}
user.setRoles(roles);
}
sessionRegistry.registerNewSession(value, authentication.getPrincipal()); sessionRegistry.registerNewSession(value, authentication.getPrincipal());
httpServletResponse.setStatus(200); httpServletResponse.setStatus(200);
httpServletResponse.setContentType("application/json; charset=utf-8"); httpServletResponse.setContentType("application/json; charset=utf-8");
httpServletResponse.getWriter().println(new ObjectMapper().writeValueAsString("登陆成功")); httpServletResponse.getWriter().println(new ObjectMapper().writeValueAsString(user));
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论