计算机系统应用教程网站

网站首页 > 技术文章 正文

spring boot +security+oauth服务器和资源服务器(注解实现)

btikc 2024-09-25 15:17:10 技术文章 20 ℃ 0 评论

部分代码是搬别人写好、自己做了调整

一、认证服务器配置

1.新建maven项目pom.xml

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-oauth2</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-security</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>

</dependencies>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

2.导入sql脚本(用户与角色相关的sql)

-- ----------------------------

-- Table structure for rc_menu

-- ----------------------------

DROP TABLE IF EXISTS `rc_menu`;

CREATE TABLE `rc_menu` (

`id` varchar(64) NOT NULL,

`code` varchar(255) DEFAULT NULL COMMENT '菜单编码',

`p_code` varchar(255) DEFAULT NULL COMMENT '菜单父编码',

`p_id` varchar(255) DEFAULT NULL COMMENT '父菜单ID',

`name` varchar(255) DEFAULT NULL COMMENT '名称',

`url` varchar(255) DEFAULT NULL COMMENT '请求地址',

`is_menu` int(11) DEFAULT NULL COMMENT '是否是菜单',

`level` int(11) DEFAULT NULL COMMENT '菜单层级',

`sort` int(11) DEFAULT NULL COMMENT '菜单排序',

`status` int(11) DEFAULT NULL,

`icon` varchar(255) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `FK_CODE` (`code`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of rc_menu

-- ----------------------------

INSERT INTO `rc_menu` VALUES ('000000000000000000', 'root', '0', '0', '系统根目录', '', '1', '0', '1', '1', null, '2017-08-03 18:31:54', null);

INSERT INTO `rc_menu` VALUES ('893287144657780736', 'system', 'root', '000000000000000000', '系统设置', 'system', '1', '1', '10', '1', '', '2017-08-04 09:47:06', null);

INSERT INTO `rc_menu` VALUES ('893288715881807872', 'userList', 'system', '893287144657780736', '用户管理', 'user/list', '1', '2', '1', '1', '', '2017-08-04 09:53:21', '2017-08-07 18:18:39');

INSERT INTO `rc_menu` VALUES ('893304960282787840', 'user/add', 'userList', '893288715881807872', '用户添加', 'user/add', '0', '3', '1', '1', '', '2017-08-04 10:57:54', '2017-08-08 11:02:55');

INSERT INTO `rc_menu` VALUES ('894396523532517376', 'user/edit', 'userList', '893288715881807872', '用户修改', 'user/edit', '0', '3', '1', '1', '', '2017-08-07 11:15:23', '2017-08-07 16:57:52');

INSERT INTO `rc_menu` VALUES ('894473486712438784', 'user/view', 'userList', '893288715881807872', '用户查看', 'user/View', '0', '3', '2', '1', '', '2017-08-07 16:21:12', null);

INSERT INTO `rc_menu` VALUES ('894473651837992960', 'user/delete', 'userList', '893288715881807872', '用户删除', 'user/delete', '0', '3', '4', '1', '', '2017-08-07 16:21:52', null);

INSERT INTO `rc_menu` VALUES ('894475142061621248', 'roleList', 'system', '893287144657780736', '角色管理', 'role/list', '1', '2', '2', '1', '', '2017-08-07 16:27:47', '2017-08-08 10:34:56');

INSERT INTO `rc_menu` VALUES ('894475827880656896', 'role/add', 'roleList', '894475142061621248', '角色添加', 'role/add', '0', '3', '1', '1', '', '2017-08-07 16:30:31', null);

INSERT INTO `rc_menu` VALUES ('894475985452269568', 'role/edit', 'roleList', '894475142061621248', '角色编辑', 'role/edit', '0', '3', '2', '1', '', '2017-08-07 16:31:08', null);

INSERT INTO `rc_menu` VALUES ('894476118730473472', 'role/delete', 'roleList', '894475142061621248', '角色删除', 'role/delete', '0', '3', '2', '1', '', '2017-08-07 16:31:40', '2017-08-07 16:37:24');

INSERT INTO `rc_menu` VALUES ('894476276402749440', 'role/permission', 'roleList', '894475142061621248', '角色配权', 'role/permission', '0', '3', '3', '1', '', '2017-08-07 16:32:18', null);

INSERT INTO `rc_menu` VALUES ('894476950951690240', 'menu/list', 'system', '893287144657780736', '菜单管理', 'menu/list', '1', '2', '2', '1', '', '2017-08-07 16:34:58', null);

INSERT INTO `rc_menu` VALUES ('894477107919323136', 'menu/add', 'menu/list', '894476950951690240', '菜单添加', 'menu/add', '0', '3', '1', '1', '', '2017-08-07 16:35:36', null);

INSERT INTO `rc_menu` VALUES ('894477244926263296', 'menu/edit', 'menu/list', '894476950951690240', '菜单编辑', 'menu/edit', '0', '3', '2', '1', '', '2017-08-07 16:36:08', null);

INSERT INTO `rc_menu` VALUES ('894477420512411648', 'menu/delete', 'menu/list', '894476950951690240', '菜单删除', 'menu/delete', '0', '3', '2', '1', '', '2017-08-07 16:36:50', null);

INSERT INTO `rc_menu` VALUES ('894477851082883072', 'apidoc', 'system', '893287144657780736', 'Api文档', 'swagger-ui.html', '1', '2', '9', '1', '', '2017-08-07 16:38:33', '2017-09-13 11:20:26');

INSERT INTO `rc_menu` VALUES ('894477995903811584', 'database/log', 'system', '893287144657780736', '数据库日志', 'druid', '1', '2', '10', '1', '', '2017-08-07 16:39:07', '2017-08-08 09:56:29');

INSERT INTO `rc_menu` VALUES ('894752734459199488', 'companyList', 'root', '000000000000000000', '公司管理', 'companyList', '1', '1', '1', '1', '', '2017-08-08 10:50:50', null);

INSERT INTO `rc_menu` VALUES ('903459378655395840', '/user/modify', 'userList', '893288715881807872', '密码重置', '/user/modify', '1', '3', '2', '1', '', '2017-09-01 11:27:56', null);

-- ----------------------------

-- Table structure for rc_privilege

-- ----------------------------

DROP TABLE IF EXISTS `rc_privilege`;

CREATE TABLE `rc_privilege` (

`role_id` int(11) NOT NULL,

`menu_id` varchar(255) NOT NULL,

`create_time` datetime DEFAULT NULL,

PRIMARY KEY (`role_id`,`menu_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of rc_privilege

-- ----------------------------

INSERT INTO `rc_privilege` VALUES ('6', '893287144657780736', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '893288715881807872', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '893304960282787840', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '894396523532517376', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '894473486712438784', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '894473651837992960', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '894477851082883072', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('6', '894477995903811584', '2017-08-08 11:31:39');

INSERT INTO `rc_privilege` VALUES ('8', '893287144657780736', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '893288715881807872', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '893304960282787840', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894396523532517376', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894473486712438784', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894473651837992960', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894475142061621248', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894475827880656896', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894475985452269568', '2017-08-08 11:56:44');

INSERT INTO `rc_privilege` VALUES ('8', '894476118730473472', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894476276402749440', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894476950951690240', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894477107919323136', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894477244926263296', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894477420512411648', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894477851082883072', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894477995903811584', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894752734459199488', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('8', '894769217763540992', '2017-08-08 11:56:45');

INSERT INTO `rc_privilege` VALUES ('17', '893287144657780736', '2017-09-14 18:40:48');

INSERT INTO `rc_privilege` VALUES ('17', '894477851082883072', '2017-09-14 18:40:51');

INSERT INTO `rc_privilege` VALUES ('17', '894477995903811584', '2017-09-14 18:40:53');

INSERT INTO `rc_privilege` VALUES ('17', '894752734459199488', '2017-09-14 18:40:54');

-- ----------------------------

-- Table structure for rc_role

-- ----------------------------

DROP TABLE IF EXISTS `rc_role`;

CREATE TABLE `rc_role` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(255) DEFAULT NULL,

`value` varchar(255) DEFAULT NULL,

`tips` varchar(255) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

`status` int(11) NOT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `unique_role_name` (`name`),

UNIQUE KEY `unique_role_value` (`value`)

) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of rc_role

-- ----------------------------

INSERT INTO `rc_role` VALUES ('6', '管理员', 'admin', null, '2017-06-20 15:07:13', '2017-06-26 12:46:09', '1');

INSERT INTO `rc_role` VALUES ('8', '超级管理员', 'super', null, '2017-06-20 15:08:45', null, '1');

INSERT INTO `rc_role` VALUES ('17', '用户', 'user', null, '2017-06-28 18:50:39', '2017-07-21 09:41:28', '1');

-- ----------------------------

-- Table structure for rc_user

-- ----------------------------

DROP TABLE IF EXISTS `rc_user`;

CREATE TABLE `rc_user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`avatar` varchar(255) DEFAULT NULL,

`username` varchar(45) DEFAULT NULL,

`password` varchar(96) DEFAULT NULL,

`salt` varchar(45) DEFAULT NULL,

`name` varchar(45) DEFAULT NULL,

`birthday` datetime DEFAULT NULL,

`sex` int(11) DEFAULT NULL,

`email` varchar(45) DEFAULT NULL,

`phone` varchar(45) DEFAULT NULL,

`status` int(11) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `unique_user_username` (`username`)

) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of rc_user

-- ----------------------------

INSERT INTO `rc_user` VALUES ('46', null, 'super', '$2a$10$cKRbR9IJktfmKmf/wShyo.5.J8IxO/7YVn8twuWFtvPgruAF8gtKq', null, '超级管理员', '2017-06-22 14:26:09', '1', null, null, '1', '2017-06-20 15:12:16', '2017-09-12 14:39:48');

INSERT INTO `rc_user` VALUES ('48', null, 'admin', '$2a$10$cKRbR9IJktfmKmf/wShyo.5.J8IxO/7YVn8twuWFtvPgruAF8gtKq', null, '管理员', null, '1', null, null, '1', '2017-06-26 17:31:41', null);

INSERT INTO `rc_user` VALUES ('49', null, 'yangxiufeng', '$2a$10$cKRbR9IJktfmKmf/wShyo.5.J8IxO/7YVn8twuWFtvPgruAF8gtKq', null, '秀秀1', null, '1', null, null, '1', '2017-08-30 10:34:59', '2017-09-18 16:10:22');

INSERT INTO `rc_user` VALUES ('50', null, 'test1', '$2a$10$cKRbR9IJktfmKmf/wShyo.5.J8IxO/7YVn8twuWFtvPgruAF8gtKq', null, 'test1', null, '1', null, null, '1', '2017-09-18 16:11:15', null);

INSERT INTO `rc_user` VALUES ('51', null, 'test2', '$2a$10$cKRbR9IJktfmKmf/wShyo.5.J8IxO/7YVn8twuWFtvPgruAF8gtKq', null, 'test2', null, '1', null, null, '1', '2017-09-21 17:09:51', null);

-- ----------------------------

-- Table structure for rc_user_role

-- ----------------------------

DROP TABLE IF EXISTS `rc_user_role`;

CREATE TABLE `rc_user_role` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`user_id` int(11) DEFAULT NULL,

`role_id` int(11) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`create_by` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of rc_user_role

-- ----------------------------

INSERT INTO `rc_user_role` VALUES ('1', '46', '8', '2017-09-11 13:02:45', null);

INSERT INTO `rc_user_role` VALUES ('2', '48', '6', '2017-09-11 13:02:56', null);

INSERT INTO `rc_user_role` VALUES ('3', '49', '17', '2017-09-11 13:03:12', null);

INSERT INTO `rc_user_role` VALUES ('19', '50', '6', '2017-09-12 14:20:20', '超级管理员');

INSERT INTO `rc_user_role` VALUES ('20', '50', '17', '2017-09-12 14:20:20', '超级管理员');

INSERT INTO `rc_user_role` VALUES ('22', '57', '8', '2017-09-18 16:34:58', '超级管理员');

INSERT INTO `rc_user_role` VALUES ('23', '57', '17', '2017-09-18 16:34:58', '超级管理员');

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170

2.通过javaBean配置

AuthorizationServerConfig.java

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired

private AuthenticationManager authenticationManager;

/* @Autowired

private DataSource dataSource;*/

@Autowired

private UserDetailsServiceImpl userDetailsService;

@Autowired

private RedisConnectionFactory redisConnectionFactory;

@Bean

RedisTokenStore redisTokenStore(){

return new RedisTokenStore(redisConnectionFactory);

}

//token存储数据库

// @Bean

// public JdbcTokenStore jdbcTokenStore(){

// return new JdbcTokenStore(dataSource);

// }

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

//clients.withClientDetails(clientDetails());

clients.inMemory().withClient("app").secret("app").scopes("app").authorizedGrantTypes("password","refresh_token");

}

/* @Bean

public ClientDetailsService clientDetails() {

return new JdbcClientDetailsService(dataSource);

}*/

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.tokenStore(redisTokenStore())

.userDetailsService(userDetailsService)

.authenticationManager(authenticationManager);

endpoints.tokenServices(defaultTokenServices());

}

/**

* <p>注意,自定义TokenServices的时候,需要设置@Primary,否则报错,</p>

* @return

*/

@Primary

@Bean

public DefaultTokenServices defaultTokenServices(){

DefaultTokenServices tokenServices = new DefaultTokenServices();

tokenServices.setTokenStore(redisTokenStore());

tokenServices.setSupportRefreshToken(true);

//tokenServices.setClientDetailsService(clientDetails());

tokenServices.setAccessTokenValiditySeconds(60*60*12); // token有效期自定义设置,默认12小时

tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);//默认30天,这里修改

return tokenServices;

}

@Override

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

security.tokenKeyAccess("permitAll()");

security .checkTokenAccess("isAuthenticated()");

security.allowFormAuthenticationForClients();

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

ResourceServerConfig.java

@Configuration

@EnableResourceServer

public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

@Override

public void configure(HttpSecurity http) throws Exception {

http.

csrf().disable()

.exceptionHandling()

.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\""))

.and()

.authorizeRequests().anyRequest().authenticated()

.and()

.httpBasic();

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

WebSecurityConfig.java

@Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private UserDetailsServiceImpl userDetailsService;

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder();

}

@Override

@Bean

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService)

.passwordEncoder(passwordEncoder());

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

.anyRequest().authenticated()

.and()

.formLogin().and()

.csrf().disable()

.httpBasic();

}

@Override

public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers("/favor.ioc");

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

UserDetailsServiceImpl.java

@Service

public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired

private UserService userService;

@Autowired

private RoleService roleService;

@Autowired

private PermissionService permissionService;

@Override

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

RcUserEntity userEntity = userService.findByUsername(username);

if (userEntity == null) {

throw new UsernameNotFoundException("用户:" + username + ",不存在!");

}

Set<GrantedAuthority> grantedAuthorities = new HashSet<>();

boolean enabled = true; // 可用性 :true:可用 false:不可用

boolean accountNonExpired = true; // 过期性 :true:没过期 false:过期

boolean credentialsNonExpired = true; // 有效性 :true:凭证有效 false:凭证无效

boolean accountNonLocked = true; // 锁定性 :true:未锁定 false:已锁定

List<RcRoleEntity> roleValues = roleService.getRoleValuesByUserId(userEntity.getId());

for (RcRoleEntity role:roleValues){

//角色必须是ROLE_开头,可以在数据库中设置

GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_"+role.getValue());

grantedAuthorities.add(grantedAuthority);

//获取权限

List<RcMenuEntity> permissionList = permissionService.getPermissionsByRoleId(role.getId());

for (RcMenuEntity menu:permissionList) {

GrantedAuthority authority = new SimpleGrantedAuthority(menu.getCode());

grantedAuthorities.add(authority);

}

}

User user = new User(userEntity.getUsername(), userEntity.getPassword(),

enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorities);

return user;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

RevokeTokenEndpoint.java

@FrameworkEndpoint

public class RevokeTokenEndpoint {

@Autowired

private ConsumerTokenServices consumerTokenServices;

@RequestMapping(value = "/oauth/token", method= RequestMethod.DELETE)

public @ResponseBody

Msg revokeToken(String access_token){

Msg msg = new Msg();

if (consumerTokenServices.revokeToken(access_token)){

msg.setCode(Msg.SUCCESS);

msg.setMsg("注销成功");

}else {

msg.setCode(Msg.FAILED);

msg.setMsg("注销失败");

}

return msg;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Msg.java

public class Msg implements Serializable{

private static final long serialVersionUID = 7514826298158585250L;

public static final int SUCCESS=200;

public static final int FAILED=201;

private Integer code;

private String msg;

public Integer getCode() {

return code;

}

public void setCode(Integer code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

entity

RcMenuEntity.java

@Entity

@Table(name = "rc_menu")

public class RcMenuEntity {

private String id;

private String code;

private String pCode;

private String pId;

private String name;

private String url;

private Integer isMenu;

private Integer level;

private Integer sort;

private Integer status;

private String icon;

private Date createTime;

private Date updateTime;

@Id

@Column(name = "id")

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

@Basic

@Column(name = "code")

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

@Basic

@Column(name = "p_code")

public String getpCode() {

return pCode;

}

public void setpCode(String pCode) {

this.pCode = pCode;

}

@Basic

@Column(name = "p_id")

public String getpId() {

return pId;

}

public void setpId(String pId) {

this.pId = pId;

}

@Basic

@Column(name = "name")

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Basic

@Column(name = "url")

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

@Basic

@Column(name = "is_menu")

public Integer getIsMenu() {

return isMenu;

}

public void setIsMenu(Integer isMenu) {

this.isMenu = isMenu;

}

@Basic

@Column(name = "level")

public Integer getLevel() {

return level;

}

public void setLevel(Integer level) {

this.level = level;

}

@Basic

@Column(name = "sort")

public Integer getSort() {

return sort;

}

public void setSort(Integer sort) {

this.sort = sort;

}

@Basic

@Column(name = "status")

public Integer getStatus() {

return status;

}

public void setStatus(Integer status) {

this.status = status;

}

@Basic

@Column(name = "icon")

public String getIcon() {

return icon;

}

public void setIcon(String icon) {

this.icon = icon;

}

@Basic

@Column(name = "create_time")

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Basic

@Column(name = "update_time")

public Date getUpdateTime() {

return updateTime;

}

public void setUpdateTime(Date updateTime) {

this.updateTime = updateTime;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

RcMenuEntity that = (RcMenuEntity) o;

if (id != null ? !id.equals(that.id) : that.id != null) return false;

if (code != null ? !code.equals(that.code) : that.code != null) return false;

if (pCode != null ? !pCode.equals(that.pCode) : that.pCode != null) return false;

if (pId != null ? !pId.equals(that.pId) : that.pId != null) return false;

if (name != null ? !name.equals(that.name) : that.name != null) return false;

if (url != null ? !url.equals(that.url) : that.url != null) return false;

if (isMenu != null ? !isMenu.equals(that.isMenu) : that.isMenu != null) return false;

if (level != null ? !level.equals(that.level) : that.level != null) return false;

if (sort != null ? !sort.equals(that.sort) : that.sort != null) return false;

if (status != null ? !status.equals(that.status) : that.status != null) return false;

if (icon != null ? !icon.equals(that.icon) : that.icon != null) return false;

if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;

if (updateTime != null ? !updateTime.equals(that.updateTime) : that.updateTime != null) return false;

return true;

}

@Override

public int hashCode() {

int result = id != null ? id.hashCode() : 0;

result = 31 * result + (code != null ? code.hashCode() : 0);

result = 31 * result + (pCode != null ? pCode.hashCode() : 0);

result = 31 * result + (pId != null ? pId.hashCode() : 0);

result = 31 * result + (name != null ? name.hashCode() : 0);

result = 31 * result + (url != null ? url.hashCode() : 0);

result = 31 * result + (isMenu != null ? isMenu.hashCode() : 0);

result = 31 * result + (level != null ? level.hashCode() : 0);

result = 31 * result + (sort != null ? sort.hashCode() : 0);

result = 31 * result + (status != null ? status.hashCode() : 0);

result = 31 * result + (icon != null ? icon.hashCode() : 0);

result = 31 * result + (createTime != null ? createTime.hashCode() : 0);

result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);

return result;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190

RcPrivilegeEntity.java

@Entity

@Table(name = "rc_privilege")

public class RcPrivilegeEntity implements Serializable{

private static final long serialVersionUID = 7945786697073389306L;

private Integer roleId;

private String menuId;

private Date createTime;

@Id

@Column(name = "role_id")

public Integer getRoleId() {

return roleId;

}

public void setRoleId(Integer roleId) {

this.roleId = roleId;

}

@Basic

@Column(name = "menu_id")

public String getMenuId() {

return menuId;

}

public void setMenuId(String menuId) {

this.menuId = menuId;

}

@Basic

@Column(name = "create_time")

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

RcPrivilegeEntity that = (RcPrivilegeEntity) o;

if (roleId != null ? !roleId.equals(that.roleId) : that.roleId != null) return false;

if (menuId != null ? !menuId.equals(that.menuId) : that.menuId != null) return false;

if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;

return true;

}

@Override

public int hashCode() {

int result = roleId != null ? roleId.hashCode() : 0;

result = 31 * result + (menuId != null ? menuId.hashCode() : 0);

result = 31 * result + (createTime != null ? createTime.hashCode() : 0);

return result;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

RcRoleEntity.java

@Entity

@Table(name = "rc_role")

public class RcRoleEntity {

private int id;

private String name;

private String value;

private String tips;

private Date createTime;

private Date updateTime;

private int status;

@Id

@Column(name = "id")

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Basic

@Column(name = "name")

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Basic

@Column(name = "value")

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

@Basic

@Column(name = "tips")

public String getTips() {

return tips;

}

public void setTips(String tips) {

this.tips = tips;

}

@Basic

@Column(name = "create_time")

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Basic

@Column(name = "update_time")

public Date getUpdateTime() {

return updateTime;

}

public void setUpdateTime(Date updateTime) {

this.updateTime = updateTime;

}

@Basic

@Column(name = "status")

public int getStatus() {

return status;

}

public void setStatus(int status) {

this.status = status;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

RcRoleEntity that = (RcRoleEntity) o;

if (id != that.id) return false;

if (status != that.status) return false;

if (name != null ? !name.equals(that.name) : that.name != null) return false;

if (value != null ? !value.equals(that.value) : that.value != null) return false;

if (tips != null ? !tips.equals(that.tips) : that.tips != null) return false;

if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;

if (updateTime != null ? !updateTime.equals(that.updateTime) : that.updateTime != null) return false;

return true;

}

@Override

public int hashCode() {

int result = id;

result = 31 * result + (name != null ? name.hashCode() : 0);

result = 31 * result + (value != null ? value.hashCode() : 0);

result = 31 * result + (tips != null ? tips.hashCode() : 0);

result = 31 * result + (createTime != null ? createTime.hashCode() : 0);

result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);

result = 31 * result + status;

return result;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

RcUserEntity.java

@Entity

@Table(name = "rc_user")

public class RcUserEntity {

private int id;

private String avatar;

private String username;

private String password;

private String salt;

private String name;

private Date birthday;

private Integer sex;

private String email;

private String phone;

private Integer status;

private Date createTime;

private Date updateTime;

@Id

@Column(name = "id")

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Basic

@Column(name = "avatar")

public String getAvatar() {

return avatar;

}

public void setAvatar(String avatar) {

this.avatar = avatar;

}

@Basic

@Column(name = "username")

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

@Basic

@Column(name = "password")

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Basic

@Column(name = "salt")

public String getSalt() {

return salt;

}

public void setSalt(String salt) {

this.salt = salt;

}

@Basic

@Column(name = "name")

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Basic

@Column(name = "birthday")

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

@Basic

@Column(name = "sex")

public Integer getSex() {

return sex;

}

public void setSex(Integer sex) {

this.sex = sex;

}

@Basic

@Column(name = "email")

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Basic

@Column(name = "phone")

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

@Basic

@Column(name = "status")

public Integer getStatus() {

return status;

}

public void setStatus(Integer status) {

this.status = status;

}

@Basic

@Column(name = "create_time")

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Basic

@Column(name = "update_time")

public Date getUpdateTime() {

return updateTime;

}

public void setUpdateTime(Date updateTime) {

this.updateTime = updateTime;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

RcUserEntity that = (RcUserEntity) o;

if (id != that.id) return false;

if (avatar != null ? !avatar.equals(that.avatar) : that.avatar != null) return false;

if (username != null ? !username.equals(that.username) : that.username != null) return false;

if (password != null ? !password.equals(that.password) : that.password != null) return false;

if (salt != null ? !salt.equals(that.salt) : that.salt != null) return false;

if (name != null ? !name.equals(that.name) : that.name != null) return false;

if (birthday != null ? !birthday.equals(that.birthday) : that.birthday != null) return false;

if (sex != null ? !sex.equals(that.sex) : that.sex != null) return false;

if (email != null ? !email.equals(that.email) : that.email != null) return false;

if (phone != null ? !phone.equals(that.phone) : that.phone != null) return false;

if (status != null ? !status.equals(that.status) : that.status != null) return false;

if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;

if (updateTime != null ? !updateTime.equals(that.updateTime) : that.updateTime != null) return false;

return true;

}

@Override

public int hashCode() {

int result = id;

result = 31 * result + (avatar != null ? avatar.hashCode() : 0);

result = 31 * result + (username != null ? username.hashCode() : 0);

result = 31 * result + (password != null ? password.hashCode() : 0);

result = 31 * result + (salt != null ? salt.hashCode() : 0);

result = 31 * result + (name != null ? name.hashCode() : 0);

result = 31 * result + (birthday != null ? birthday.hashCode() : 0);

result = 31 * result + (sex != null ? sex.hashCode() : 0);

result = 31 * result + (email != null ? email.hashCode() : 0);

result = 31 * result + (phone != null ? phone.hashCode() : 0);

result = 31 * result + (status != null ? status.hashCode() : 0);

result = 31 * result + (createTime != null ? createTime.hashCode() : 0);

result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);

return result;

}

@Override

public String toString() {

return "RcUserEntity{" +

"id=" + id +

", avatar='" + avatar + '\'' +

", username='" + username + '\'' +

", password='" + password + '\'' +

", salt='" + salt + '\'' +

", name='" + name + '\'' +

", birthday=" + birthday +

", sex=" + sex +

", email='" + email + '\'' +

", phone='" + phone + '\'' +

", status=" + status +

", createTime=" + createTime +

", updateTime=" + updateTime +

'}';

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208

RcUserRoleEntity.java

@Entity

@Table(name = "rc_user_role")

public class RcUserRoleEntity implements Serializable{

private static final long serialVersionUID = 6803189083763570768L;

private int id;

private Integer userId;

private Integer roleId;

private Date createTime;

private String createBy;

@Id

@Column(name = "id")

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Id

@Column(name = "user_id")

public Integer getUserId() {

return userId;

}

public void setUserId(Integer userId) {

this.userId = userId;

}

@Basic

@Column(name = "role_id")

public Integer getRoleId() {

return roleId;

}

public void setRoleId(Integer roleId) {

this.roleId = roleId;

}

@Basic

@Column(name = "create_time")

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Basic

@Column(name = "create_by")

public String getCreateBy() {

return createBy;

}

public void setCreateBy(String createBy) {

this.createBy = createBy;

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

RcUserRoleEntity that = (RcUserRoleEntity) o;

if (id != that.id) return false;

if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;

if (roleId != null ? !roleId.equals(that.roleId) : that.roleId != null) return false;

if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) return false;

if (createBy != null ? !createBy.equals(that.createBy) : that.createBy != null) return false;

return true;

}

@Override

public int hashCode() {

int result = id;

result = 31 * result + (userId != null ? userId.hashCode() : 0);

result = 31 * result + (roleId != null ? roleId.hashCode() : 0);

result = 31 * result + (createTime != null ? createTime.hashCode() : 0);

result = 31 * result + (createBy != null ? createBy.hashCode() : 0);

return result;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

PermissionRepository.java

@Repository

public interface PermissionRepository extends JpaRepository<RcMenuEntity,Integer> {

@Query(value = "select menu.* from rc_menu menu,rc_privilege p where menu.id=p.menu_id and p.role_id=?1",nativeQuery = true)

List<RcMenuEntity> getPermissionsByRoleId(Integer roleId);

}

  • 1
  • 2
  • 3
  • 4
  • 5

RoleRepository.java

@Repository

public interface RoleRepository extends JpaRepository<RcRoleEntity,Integer>{

@Query(value = "select role.* from rc_role role,rc_user_role ur where role.id=ur.role_id and ur.user_id=?1",nativeQuery = true)

List<RcRoleEntity> getRoleValuesByUserId(Integer userId);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

UserRepository.java

@Repository

public interface UserRepository extends JpaRepository<RcUserEntity,Integer>{

RcUserEntity findByUsername(String username);

}

  • 1
  • 2
  • 3
  • 4

PermissionServiceImpl.java

@Service

public class PermissionServiceImpl implements PermissionService {

@Autowired

private PermissionRepository permissionRepository;

@Override

public List<RcMenuEntity> getPermissionsByRoleId(Integer roleId) {

return permissionRepository.getPermissionsByRoleId(roleId);

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

RoleServiceImpl.java

@Service

public class RoleServiceImpl implements RoleService {

@Autowired

private RoleRepository roleRepository;

@Override

public List<RcRoleEntity> getRoleValuesByUserId(Integer userId) {

return roleRepository.getRoleValuesByUserId(userId);

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

UserServiceImpl.java

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserRepository userRepository;

@Override

public RcUserEntity findByUsername(String username) {

return userRepository.findByUsername(username);

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

UserController.java

@RestController

public class UserController {

@RequestMapping("/user")

public Principal user(Principal user) {

return user;

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

application.yml

server:

port: 9060

spring:

application:

name: auth2.0-center

# zipkin:

# base-url: http://10.10.8.2:9050

jpa:

show-sql: true

datasource:

url: jdbc:mysql://192.168.122.128:3306/zuul_auth?useUnicode=true&characterEncoding=utf-8

username: xx

password: xxxx

druid:

driver-class-name: com.mysql.jdbc.Driver

redis:

host: 127.0.0.1

port: 6379

# password: 123456

#eureka:

# instance:

# prefer-ip-address: true #???IP???

# instance-id: ${spring.cloud.client.ipAddress}:${server.port}

# client:

# service-url:

# defaultZone: http://127.0.0.1:9010/eureka/

###actuator???? start####

endpoints:

health:

sensitive: false

enabled: true

##???????o????????????????????401:Unauthorized

management:

security:

enabled: false

###actuator???? end####

security:

oauth2:

resource:

filter-order: 3

logging:

config: classpath:logback.xml

level:

org:

springframework:

web: info

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

到这里认证服务器代码已基本完成,省略了些接口,下面我们看下资源服务器的配置

二、资源服务器

ResourceServerConfig.java

@Configuration

@EnableResourceServer

public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override

public void configure(HttpSecurity http) throws Exception {

/* http.

csrf().disable()

.exceptionHandling()

.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\""))

.and()

.authorizeRequests().anyRequest().authenticated()

.and()

.httpBasic();*/

http.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.NEVER)

.and()

.requestMatchers()

.antMatchers("/**")

.and()

.authorizeRequests()

.filterSecurityInterceptorOncePerRequest(true)

.antMatchers("/api/**").permitAll()

.and().headers().frameOptions().disable();

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

UserController.java

@RestController

public class UserController {

@GetMapping(value = "getUser")

@PreAuthorize("hasAnyAuthority('user/view')")

@ResponseBody

public String getUser(){

return "order";

}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

application.yml

server:

port: 9023

spring:

application:

name: resource

# zipkin:

# base-url: http://10.10.8.2:9050

#eureka:

# instance:

# prefer-ip-address: true #

# instance-id: ${spring.cloud.client.ipAddress}:${server.port}

# client:

# service-url:

# defaultZone: http://127.0.0.1:9010/eureka/

###actuator???? start####

endpoints:

health:

sensitive: false

enabled: true

##???????o????????????????????401:Unauthorized

management:

security:

enabled: false

###actuator???? end####

security:

oauth2:

resource:

id: resource

user-info-uri: http://127.0.0.1:9060/user

prefer-token-info: false

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

三、演示

1.获取token,相当于我们实际项目中的登录功能

2.请求资源(带token)

3.不带token请求

在实际项目中,我们和前端交互一版使用json格式,会对异常进行处理,同时spring security支持三种地方携带token,具体可以查看源码。代码就不做过多解释了。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表