新版
This commit is contained in:
parent
4cfba5edc5
commit
4c1c168be7
@ -1,14 +1,13 @@
|
|||||||
package com.zsc.edu.bill.framework.security;
|
package com.zsc.edu.bill.framework.security;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.time.LocalDateTime;
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author harry_yao
|
* @author harry_yao
|
||||||
@ -22,7 +21,7 @@ public class CustomAuthenticationSuccessHandler implements AuthenticationSuccess
|
|||||||
// private final LoginLogService loginLogService;
|
// private final LoginLogService loginLogService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||||
// Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
// Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||||
// String sessionId = request.getRequestedSessionId();
|
// String sessionId = request.getRequestedSessionId();
|
||||||
// String remoteAddr = request.getRemoteAddr();
|
// String remoteAddr = request.getRemoteAddr();
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package com.zsc.edu.bill.framework.security;
|
package com.zsc.edu.bill.framework.security;
|
||||||
|
|
||||||
|
import com.zsc.edu.bill.modules.system.entity.Authority;
|
||||||
|
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
|
||||||
import com.zsc.edu.bill.modules.system.entity.User;
|
import com.zsc.edu.bill.modules.system.entity.User;
|
||||||
|
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity;
|
||||||
import com.zsc.edu.bill.modules.system.repo.UserRepository;
|
import com.zsc.edu.bill.modules.system.repo.UserRepository;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -9,6 +12,10 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author harry_yao
|
* @author harry_yao
|
||||||
*/
|
*/
|
||||||
@ -17,11 +24,20 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
public class JpaUserDetailsServiceImpl implements UserDetailsService {
|
public class JpaUserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
|
||||||
private final UserRepository userRepo;
|
private final UserRepository userRepo;
|
||||||
|
private final RoleAuthoritiesReposity roleAuthoritiesReposity;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
User user = userRepo.findByUsername(username);
|
User user = userRepo.selectByUsername(username);
|
||||||
|
|
||||||
|
List<RoleAuthority> authorities= roleAuthoritiesReposity.selectByRoleId(user.getRoleId());
|
||||||
|
Set<Authority> authorities1=new HashSet<>();
|
||||||
|
authorities.stream().forEach(authority ->authorities1.add(Authority.valueOf(authority.getAuthority())));
|
||||||
|
user.role.authorities=authorities1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// .orElseThrow(() ->
|
// .orElseThrow(() ->
|
||||||
// new UsernameNotFoundException("用户 '" + username + "' 不存在!")
|
// new UsernameNotFoundException("用户 '" + username + "' 不存在!")
|
||||||
// );
|
// );
|
||||||
|
@ -17,7 +17,7 @@ public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilt
|
|||||||
@Override
|
@Override
|
||||||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws AuthenticationException {
|
throws AuthenticationException {
|
||||||
if (!request.getMethod().equals("POST")) {
|
if (!request.getMethod().equals("POST")) {
|
||||||
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
||||||
}
|
}
|
||||||
if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
|
if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
|
||||||
|
@ -11,4 +11,5 @@ import org.mapstruct.ReportingPolicy;
|
|||||||
*/
|
*/
|
||||||
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||||
public interface RoleMapper extends BaseMapper<RoleDto, Role> {
|
public interface RoleMapper extends BaseMapper<RoleDto, Role> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,9 @@ package com.zsc.edu.bill.modules.system.repo;
|
|||||||
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
|
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public interface RoleAuthoritiesReposity extends BaseMapper<RoleAuthority> {
|
public interface RoleAuthoritiesReposity extends BaseMapper<RoleAuthority> {
|
||||||
|
List<RoleAuthority> selectByRoleId(Long roleId);
|
||||||
}
|
}
|
@ -24,6 +24,11 @@ public interface UserRepository extends BaseMapper<User> {
|
|||||||
"join sys_role r on r.id = sur.role_id ${ew.customSqlSegment}")
|
"join sys_role r on r.id = sur.role_id ${ew.customSqlSegment}")
|
||||||
Page<UserVo> page(Page pageDTO, @Param("ew") QueryWrapper<User> wrapper);
|
Page<UserVo> page(Page pageDTO, @Param("ew") QueryWrapper<User> wrapper);
|
||||||
|
|
||||||
@Select("select u.* from sys_user u where u.username = ${username}")
|
// @Select("select u.*, sr.*, sra.* from sys_user u " +
|
||||||
User findByUsername(String username);
|
// "left join sys_role sr on u.role_id = sr.id " +
|
||||||
|
// "left join sys_role_authorities sra on u.role_id = sra.role_id " +
|
||||||
|
// "where u.username = #{username}")
|
||||||
|
// User findByUsername(String username);
|
||||||
|
User selectByUsername(String username);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="cn.edu.zsc.study.repository.RoleAuthoritiesReposity">
|
<mapper namespace="com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity">
|
||||||
|
<resultMap id="BaseResultMap" type="com.zsc.edu.bill.modules.system.entity.RoleAuthority">
|
||||||
|
<result column="role_id" jdbcType="BIGINT" property="roleId"/>
|
||||||
|
<result column="authority" jdbcType="BIGINT" property="authority"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="selectByRoleId" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from sys_role_authorities a
|
||||||
|
where a.role_id = #{roleId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="cn.edu.zsc.study.repository.RoleRepository">
|
<mapper namespace="com.zsc.edu.bill.modules.system.mapper.RoleMapper">
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
@ -1,5 +1,31 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="cn.edu.zsc.study.repository.UserRepository">
|
<mapper namespace="com.zsc.edu.bill.modules.system.repo.UserRepository">
|
||||||
|
<resultMap id="BaseResultMap" type="com.zsc.edu.bill.modules.system.entity.User">
|
||||||
|
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||||
|
<result column="username" jdbcType="VARCHAR" property="username"/>
|
||||||
|
<result column="password" jdbcType="VARCHAR" property="password"/>
|
||||||
|
<result column="role_id" jdbcType="BIGINT" property="roleId"/>
|
||||||
|
<result column="dept_id" jdbcType="BIGINT" property="deptId"/>
|
||||||
|
<result column="email" jdbcType="VARCHAR" property="email"/>
|
||||||
|
<result column="phone" jdbcType="VARCHAR" property="phone"/>
|
||||||
|
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
|
||||||
|
<collection property="role" ofType="com.zsc.edu.bill.modules.system.entity.Role">
|
||||||
|
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||||
|
<result column="name" jdbcType="VARCHAR" property="name"/>
|
||||||
|
</collection>
|
||||||
|
|
||||||
|
</resultMap>
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
id, username, password, email, phone, create_time
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from sys_user
|
||||||
|
left join study.sys_role sr on sys_user.role_id = sr.id
|
||||||
|
where username = #{username,jdbcType=VARCHAR}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
||||||
<mapper namespace="cn.edu.zsc.study.repository.UserRolesReposity">
|
|
||||||
</mapper>
|
|
44
src/test/java/com/zsc/edu/bill/service/UserServiceTest.java
Normal file
44
src/test/java/com/zsc/edu/bill/service/UserServiceTest.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.zsc.edu.bill.service;
|
||||||
|
|
||||||
|
import com.zsc.edu.bill.modules.system.entity.Authority;
|
||||||
|
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
|
||||||
|
import com.zsc.edu.bill.modules.system.entity.User;
|
||||||
|
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity;
|
||||||
|
import com.zsc.edu.bill.modules.system.repo.UserRepository;
|
||||||
|
import com.zsc.edu.bill.modules.system.service.UserService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ftz
|
||||||
|
* 创建时间:29/12/2023 上午11:21
|
||||||
|
* 描述: TODO
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
public class UserServiceTest {
|
||||||
|
@Resource
|
||||||
|
private RoleAuthoritiesReposity roleAuthoritiesReposity;
|
||||||
|
@Resource
|
||||||
|
private UserRepository userRepository;
|
||||||
|
@Test
|
||||||
|
void test() {
|
||||||
|
User user=userRepository.selectByUsername("admin");
|
||||||
|
List<RoleAuthority> authorities= roleAuthoritiesReposity.selectByRoleId(user.getRoleId());
|
||||||
|
Set<Authority> authorities1=new HashSet<>();
|
||||||
|
authorities.stream().forEach(authority ->authorities1.add(Authority.valueOf(authority.getAuthority())));
|
||||||
|
user.role.authorities=authorities1;
|
||||||
|
System.out.println(user);
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void test1() {
|
||||||
|
User user=userRepository.selectByUsername("admin");
|
||||||
|
System.out.println(user);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user