一 uaa认证服务配置
1.1 EnableAuthorizationServer的作用
1.1.1 作用说明
1.EnableAuthorizationServer可以用@EnableAuthorizationServer注解,并继承AuthorizationServerConfigurerAdapter来配置oauth2.0授权服务器

1.1.2 核心配置内容(*重要)
3.AuthorizationServerConfigurerAdapter要求配置以下几个类,这几个类是由Spring创建的独立的配置对象,它们会被Spring传入AuthorizationServerConfigurer中进行配置。
public class AuthorizationServerConfigurerAdapter implements AuthorizationServerConfigurer {public AuthorizationServerConfigurerAdapter() {}public void configure(ClientDetailsServiceConfigurer clients) throws Exception {}public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {}public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {}}
1.2 配置ClientDetailsServiceConfigurer客户端详情信息
1.2.1 clientDetailsServiceConfigurer配置说明
1.2.2 案例代码

1.3 AuthorizationServerTokenServices管理令牌的配置
1.3.1 令牌的配置
1.InMemoryTokenStore:这个版本的实现是被默认采用的,它可以完美的工作在单服务器上(即访问并发量压力不大的情况下,并且它在失败的时候不会进行备份),大多数的项目都可以使用这个版本的实现来进行尝试,你可以在开发的时候使用它来进行管理,因为不会被保存到磁盘中,所以更易于调试。
2.JdbcTokenStore:这是一个基于JDBC的实现版本,令牌会被保存进关系型数据库。使用这个版本的实现时,你可以在不同的服务器之间共享令牌信息,使用这个版本的时候请注意把"spring-jdbc"这个依赖加入到你的classpath当中。
3.JwtTokenStore:这个版本的全称是 JSON Web Token(JWT),它可以把令牌相关的数据进行编码(因此对于后端服务来说,它不需要进行存储,这将是一个重大优势),但是它有一个缺点,那就是撤销一个已经授权令牌将会非常困难,所以它通常用来处理一个生命周期较短的令牌以及撤销刷新令牌(refresh_token)。另外一个缺点就是这个令牌占用的空间会比较大,如果你加入了比较多用户凭证信息JwtTokenStore 不会保存任何数据,但是它在转换令牌值以及授权信息方面与 DefaultTokenServices 所扮演的角色是一样的
1.3.2 配置截图

1.3.3 代码配置
@Configuration
public class TokenConfig {
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}

3.代码如下
@Autowired
private TokenStore tokenStore;
@Autowired
private ClientDetailsService clientDetailsService;
@Bean
public AuthorizationServerTokenServices tokenService() {
DefaultTokenServices service=new DefaultTokenServices();
service.setClientDetailsService(clientDetailsService);
service.setSupportRefreshToken(true); service.setTokenStore(tokenStore);
service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
return service;
}
1.4 AuthorizationServerEndpointsConfigurer 令牌访问端点配置
AuthorizationServerEndpointsConfigurer 这个对象的实例可以完成令牌服务以及令牌endpoint配置。
1.4.1 配置授权类型(Grant Types)


1.4.2 配置授权端点的URL(Endpoint URLs)

1.4.3 代码的实现
在AuthorizationServer类中配置令牌访问端点 :

1.5 令牌端点的安全约束

1.6 授权服务配置总结:
1.7 web安全配置
1.在config目录下,新建WebSecurityconfig的配置
2.截图:

3.代码如下
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Configuration @EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
//安全拦截机制(最重要)
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/r/r1").hasAnyAuthority("p1")
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and() .formLogin() ;
}
}
二 配置详细流程
2.1 配置 SpringDataUserDetailsService
2.1.1 pom文件
pom文件新增这个两个依赖
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
2.1.2 application文件

2.1.3 mapper文件

2.1.4 dao接口
package com.ljf.springsecurity.oauth.dao;
import com.ljf.springsecurity.oauth.model.PermissionDto;
import com.ljf.springsecurity.oauth.model.UserDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
//查找用户名
public UserDto getUserByUsername(@Param("userName") String username);
//查找该用户名下的权限
public List<PermissionDto> findPermissionsByUserId(@Param("userId") String userId);
}
2.1.5 model
1.UserDto
package com.ljf.springsecurity.oauth.model;
import lombok.Data;
/**
* @author Administrator
* @version 1.0
**/
@Data
public class UserDto {
private String id;
private String username;
private String password;
private String fullname;
private String mobile;
}
2.PermissionDto
/**
* @author Administrator
* @version 1.0
**/
@Data
public class PermissionDto {
private String id;
private String code;
private String description;
private String url;
}
2.1.6 SpringDataUserDetailsService
package com.ljf.springsecurity.oauth.service;
import com.ljf.springsecurity.oauth.dao.UserMapper;
import com.ljf.springsecurity.oauth.model.PermissionDto;
import com.ljf.springsecurity.oauth.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: SpringDataUserDetailsService
* @Description: TODO
* @Author: liujianfu
* @Date: 2021/08/14 09:44:20
* @Version: V1.0
**/
@Service
public class SpringDataUserDetailsService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//将来连接数据库根据账号查询用户信息
UserDto userDto = userMapper.getUserByUsername(username);
if(userDto == null){
//如果用户查不到,返回null,由provider来抛出异常
return null;
}
//权限
// String [] authoritys={"p1"};
//根据用户的id查询用户的权限
List<PermissionDto> permissionsList = userMapper.findPermissionsByUserId(userDto.getId());
List<String> permissions = new ArrayList<>();
permissionsList.forEach(c -> permissions.add(c.getCode()));
//将permissions转成数组
String[] permissionArray = new String[permissions.size()];
permissions.toArray(permissionArray);
UserDetails userDetails = User.withUsername(userDto.getUsername()).password(userDto.getPassword()).authorities(permissionArray).build();
return userDetails;
}
}

2.1.7 启动类
package com.ljf.springsecurity.oauth;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients(basePackages = {"com.ljf.springsecurity.oauth"})
@MapperScan("com.ljf.springsecurity.oauth.dao") //@MapperScan 用户扫描MyBatis的Mapper
public class UaaApp
{
public static void main( String[] args )
{
SpringApplication.run( UaaApp.class, args);
}
}
2.2 编写AuthorizationServer
创建一个配置文件AuthorizationServer,编写客户端信息
2.2.1 客户端信息

代码案例:
//step1:客户端详情服务
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()// 使用in-memory存储
.withClient("c1")// client_id
.secret(new BCryptPasswordEncoder().encode("secret"))//客户端密钥
.resourceIds("res1")//资源列表
.authorizedGrantTypes("authorization_code", "password","client_credentials","implicit","refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials
.scopes("all")// 允许的授权范围
.autoApprove(false)//false跳转到授权页面
//加上验证回调地址
.redirectUris("http://www.baidu.com");
}
2.2.2 编写令牌管理服务
1.编写tokenconfig

2.在AuthorizationServer中配置令牌服务

2.2.4 编写令牌的端点
1.编写存储
2.编写端点
编写一个:WebSecurityConfig:在这里注册认证管理器

3.在AuthorizationServer文件中引用

2.2.5 编写令牌的约束

OAuth2.0授权服务配置详解


被折叠的 条评论
为什么被折叠?



