加入feign调用、涉及基础数据的操作抽取到admin模块

This commit is contained in:
wangiegie@gmail.com 2017-10-31 22:15:52 +08:00
parent d4fba1fdae
commit 3b0f1e7a9c
30 changed files with 471 additions and 43 deletions

View File

@ -1,9 +1,9 @@
package com.github.pig.admin;
import com.github.pig.common.web.BaseController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
/**
* @author lengleng
@ -11,6 +11,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
*/
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.github.pig.admin", "com.github.pig.common.config"})
public class PigAdminApplication {
public static void main(String[] args) {
SpringApplication.run(PigAdminApplication.class, args);

View File

@ -0,0 +1,31 @@
package com.github.pig.admin.controller;
import com.github.pig.admin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
@RestController
@RequestMapping("/menu")
public class MenuController {
@Autowired
private MenuService menuService;
/**
* 通过用户名查询用户菜单
*
* @param role 角色名称
* @return 菜单列表
*/
@GetMapping("/findMenuByRole/{role}")
public Set<String> findMenuByRole(@PathVariable String role) {
return menuService.findMenuByRole(role);
}
}

View File

@ -1,7 +1,11 @@
package com.github.pig.admin.controller;
import com.github.pig.admin.service.UserService;
import com.github.pig.common.vo.UserVo;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -12,8 +16,22 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
@Autowired
private UserService userService;
@GetMapping
public String user() {
return getUser();
}
/**
* 通过用户名查询用户及其角色信息
*
* @param username 用户名
* @return UseVo 对象
*/
@GetMapping("/findUserByUsername/{username}")
public UserVo findUserByUsername(@PathVariable String username) {
return userService.findUserByUsername(username);
}
}

View File

@ -1,4 +1,4 @@
package com.github.pig.auth.entity;
package com.github.pig.admin.entity;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package com.github.pig.auth.entity;
package com.github.pig.admin.entity;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package com.github.pig.auth.entity;
package com.github.pig.admin.entity;
import java.io.Serializable;

View File

@ -0,0 +1,25 @@
package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.github.pig.admin.entity.SysMenu;
import java.util.Set;
/**
* <p>
* 菜单权限表 Mapper 接口
* </p>
*
* @author lengleng
* @since 2017-10-29
*/
public interface SysMenuMapper extends BaseMapper<SysMenu> {
/**
* 通过角色名查询菜单
*
* @param role 角色名称
* @return 菜单列表
*/
Set<String> findMenuByRoleName(String role);
}

View File

@ -1,7 +1,7 @@
package com.github.pig.auth.mapper;
package com.github.pig.admin.mapper;
import com.github.pig.auth.entity.SysRole;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.github.pig.admin.entity.SysRole;
/**
* <p>

View File

@ -1,7 +1,7 @@
package com.github.pig.auth.mapper;
package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.github.pig.auth.entity.SysUser;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.common.vo.UserVo;
/**

View File

@ -0,0 +1,19 @@
package com.github.pig.admin.service;
import com.github.pig.admin.entity.SysMenu;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
public interface MenuService {
/**
* 通过角色名称查询URL 权限
*
* @param role 角色名称
* @return 菜单列表
*/
Set<String> findMenuByRole(String role);
}

View File

@ -0,0 +1,17 @@
package com.github.pig.admin.service;
import com.github.pig.common.vo.UserVo;
/**
* @author lengleng
* @date 2017/10/31
*/
public interface UserService {
/**
* 根据用户名查询用户角色信息
*
* @param username 用户名
* @return userVo
*/
UserVo findUserByUsername(String username);
}

View File

@ -0,0 +1,26 @@
package com.github.pig.admin.service.impl;
import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.mapper.SysMenuMapper;
import com.github.pig.admin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
@Service
public class MenuServiceImpl implements MenuService {
@Autowired
private SysMenuMapper sysMenuMapper;
@Override
@Cacheable(value = "menu_details", key = "#role +'_menu'")
public Set<String> findMenuByRole(String role) {
return sysMenuMapper.findMenuByRoleName(role);
}
}

View File

@ -0,0 +1,24 @@
package com.github.pig.admin.service.impl;
import com.github.pig.admin.mapper.SysUserMapper;
import com.github.pig.admin.service.UserService;
import com.github.pig.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* @author lengleng
* @date 2017/10/31
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private SysUserMapper sysUserMapper;
@Override
@Cacheable(value = "user_details", key = "#username")
public UserVo findUserByUsername(String username) {
return sysUserMapper.selectUserVoByUsername(username);
}
}

View File

@ -0,0 +1,164 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 从高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL -->
<!-- 日志输出规则 根据当前ROOT 级别日志输出时级别高于root默认的级别时 会输出 -->
<!-- 以下 每个配置的 filter 是过滤掉输出文件里面会出现高级别文件依然出现低级别的日志信息通过filter 过滤只记录本级别的日志-->
<!-- 属性描述 scan性设置为true时配置文件如果发生改变将会被重新加载默认值为true scanPeriod:设置监测配置文件是否有修改的时间间隔如果没有给出时间单位默认单位是毫秒。当scan为true时此属性生效。默认的时间间隔为1分钟。
debug:当此属性设置为true时将打印出logback内部日志信息实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 定义日志文件 输入位置 -->
<property name="log_dir" value="logs/ev_cmdb" />
<!-- 日志最大的历史 30天 -->
<property name="maxHistory" value="30"/>
<!-- ConsoleAppender 控制台输出日志 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 对日志进行格式化 -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern>
</encoder>
</appender>
<!-- ERROR级别日志 -->
<!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 RollingFileAppender-->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器只记录WARN级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- 最常用的滚动策略,它根据时间来制定滚动策略.既负责滚动也负责出发滚动 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志输出位置 可相对、和绝对路径 -->
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error-log.log</fileNamePattern>
<!-- 可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件假设设置每个月滚动,且<maxHistory>是6
则只保存最近6个月的文件删除之前的旧文件。注意删除旧文件是那些为了归档而创建的目录也会被删除-->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<!-- 按照固定窗口模式生成日志文件当文件大于20MB时生成新的日志文件。窗口大小是1到3当保存了3个归档文件后将覆盖最早的日志。
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy> -->
<!-- 查看当前活动文件的大小如果超过指定大小会告知RollingFileAppender 触发当前活动文件滚动
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy> -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!-- WARN级别日志 appender -->
<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器只记录WARN级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/warn-log.log
</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!-- INFO级别日志 appender -->
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器只记录INFO级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info-log.log
</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!-- DEBUG级别日志 appender -->
<appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器只记录DEBUG级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug-log.log
</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!-- TRACE级别日志 appender -->
<appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器只记录ERROR级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/trace-log.log
</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<logger name="java.sql.PreparedStatement" value="DEBUG" />
<logger name="java.sql.Connection" value="DEBUG" />
<logger name="java.sql.Statement" value="DEBUG" />
<logger name="com.ibatis" value="DEBUG" />
<logger name="com.ibatis.common.jdbc.SimpleDataSource" value="DEBUG" />
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG"/>
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />
<logger name="com.github.pig.admin" level="DEBUG" additivity="true">
<appender-ref ref="STDOUT"/>
</logger>
<!-- root级别 DEBUG -->
<root level="DEBUG">
<!-- 控制台输出 -->
<appender-ref ref="STDOUT" />
<!-- 文件输出 -->
<appender-ref ref="ERROR" />
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
<appender-ref ref="DEBUG" />
<appender-ref ref="TRACE" />
</root>
</configuration>

View File

@ -1,9 +1,9 @@
<?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="com.github.pig.auth.mapper.SysMenuMapper">
<mapper namespace="com.github.pig.admin.mapper.SysMenuMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.github.pig.auth.entity.SysMenu">
<resultMap id="BaseResultMap" type="com.github.pig.admin.entity.SysMenu">
<id column="menu_id" property="menuId" />
<result column="menu_name" property="menuName" />
<result column="menu_desc" property="menuDesc" />
@ -16,4 +16,17 @@
<result column="del_flag" property="delFlag" />
</resultMap>
<select id="findMenuByRoleName" resultType="string">
SELECT
sys_menu.url
FROM
sys_role
LEFT JOIN sys_role_menu ON sys_role_menu.role_id = sys_role.role_id
LEFT JOIN sys_menu ON sys_menu.menu_id = sys_role_menu.menu_id
WHERE
sys_role.del_flag = 0
AND sys_menu.del_flag = 0
AND sys_menu.url IS NOT NULL AND
sys_role.role_code = #{role}
</select>
</mapper>

View File

@ -1,9 +1,9 @@
<?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="com.github.pig.auth.mapper.SysRoleMapper">
<mapper namespace="com.github.pig.admin.mapper.SysRoleMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.github.pig.auth.entity.SysRole">
<resultMap id="BaseResultMap" type="com.github.pig.admin.entity.SysRole">
<id column="role_id" property="roleId" />
<result column="role_name" property="roleName" />
<result column="role_code" property="roleCode" />

View File

@ -1,10 +1,10 @@
<?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="com.github.pig.auth.mapper.SysUserMapper">
<mapper namespace="com.github.pig.admin.mapper.SysUserMapper">
<cache/>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.github.pig.auth.entity.SysUser">
<resultMap id="BaseResultMap" type="com.github.pig.admin.entity.SysUser">
<id column="user_id" property="userId"/>
<result column="username" property="username"/>
<result column="password" property="password"/>

View File

@ -28,6 +28,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -3,6 +3,7 @@ package com.github.pig.auth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@ -15,7 +16,7 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E
@SpringBootApplication
@EnableResourceServer
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.github.pig.auth","com.github.pig.common.config"})
@EnableFeignClients
public class PigAuthServerApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,23 @@
package com.github.pig.auth.feign;
import com.github.pig.auth.feign.fallback.UserServiceFallbackImpl;
import com.github.pig.common.vo.UserVo;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author lengleng
* @date 2017/10/31
*/
@FeignClient(name = "pig-admin-service", fallback = UserServiceFallbackImpl.class)
public interface UserService {
/**
* 通过用户名查询用户角色信息
*
* @param username 用户名
* @return UserVo
*/
@GetMapping("/user/findUserByUsername/{username}")
UserVo findUserByUsername(@PathVariable("username") String username);
}

View File

@ -0,0 +1,21 @@
package com.github.pig.auth.feign.fallback;
import com.github.pig.auth.feign.UserService;
import com.github.pig.common.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author lengleng
* @date 2017/10/31
* 用户服务的fallback
*/
public class UserServiceFallbackImpl implements UserService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public UserVo findUserByUsername(String username) {
logger.error("调用{}异常:{}", "findUserByUsername", username);
return null;
}
}

View File

@ -1,16 +0,0 @@
package com.github.pig.auth.mapper;
import com.github.pig.auth.entity.SysMenu;
import com.baomidou.mybatisplus.mapper.BaseMapper;
/**
* <p>
* 菜单权限表 Mapper 接口
* </p>
*
* @author lengleng
* @since 2017-10-29
*/
public interface SysMenuMapper extends BaseMapper<SysMenu> {
}

View File

@ -1,10 +1,9 @@
package com.github.pig.auth.serivce;
import com.github.pig.auth.mapper.SysUserMapper;
import com.github.pig.auth.feign.UserService;
import com.github.pig.auth.util.UserInfo;
import com.github.pig.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@ -20,12 +19,11 @@ import java.io.Serializable;
@Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailsService, Serializable {
@Autowired
private SysUserMapper sysUserMapper;
private UserService userService;
@Override
@Cacheable(value = "user_details",key = "#username + '::loadUserByUsername'")
public UserInfo loadUserByUsername(String username) throws UsernameNotFoundException {
UserVo userVo = sysUserMapper.selectUserVoByUsername(username);
UserVo userVo = userService.findUserByUsername(username);
return new UserInfo(userVo);
}
}

View File

@ -146,7 +146,7 @@
<!--<logger name="com.ibatis.common.jdbc.SimpleDataSource" value="DEBUG" />-->
<!--<logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG"/>-->
<!--<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />-->
<logger name="com.example.sbmp.mapper" level="DEBUG" additivity="true">
<logger name="com.github.pig" level="DEBUG" additivity="true">
<appender-ref ref="STDOUT"/>
</logger>

View File

@ -10,7 +10,7 @@ import org.springframework.context.annotation.Configuration;
* @date 2017/10/29
*/
@Configuration
@MapperScan("com.github.pig")
@MapperScan("com.github.pig.*.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {

View File

@ -33,6 +33,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<!--zuul网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -3,6 +3,7 @@ package com.github.pig.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@ -12,6 +13,7 @@ import org.springframework.security.config.annotation.method.configuration.Enabl
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class PigGatewayApplication {

View File

@ -0,0 +1,24 @@
package com.github.pig.gateway.feign;
import com.github.pig.gateway.feign.fallback.MenuServiceFallbackImpl;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
@FeignClient(name = "pig-admin-service", fallback = MenuServiceFallbackImpl.class)
public interface MenuService {
/**
* 通过角色名查询菜单
*
* @param role 角色名称
* @return 菜单列表
*/
@GetMapping("/menu/findMenuByRole/{role}")
Set<String> findMenuByRole(@PathVariable("role") String role);
}

View File

@ -0,0 +1,20 @@
package com.github.pig.gateway.feign.fallback;
import com.github.pig.gateway.feign.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
public class MenuServiceFallbackImpl implements MenuService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Set<String> findMenuByRole(String role) {
logger.error("调用{}异常{}","findMenuByRole",role);
return null;
}
}

View File

@ -1,15 +1,21 @@
package com.github.pig.gateway.service.impl;
import com.github.pig.gateway.feign.MenuService;
import com.github.pig.gateway.service.PermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
@ -18,20 +24,22 @@ import java.util.Set;
*/
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
@Autowired
private MenuService menuService;
@Override
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
Object principal = authentication.getPrincipal();
Collection<SimpleGrantedAuthority> grantedAuthorityList = (Collection<SimpleGrantedAuthority>) authentication.getAuthorities();
boolean hasPermission = false;
if (principal != null) {
//TODO 根据用户名查询缓存没有的查询数据库
Set<String> urls = new HashSet<>();
urls.add("/user");
//TODO 根据角色查询缓存没有的查询数据库
Set<String> urls = menuService.findMenuByRole("admin");
for (String url : urls) {
if (request.getRequestURI().contains(url)){
hasPermission =true;
if (request.getRequestURI().contains(url)) {
hasPermission = true;
break;
}
}