第一步:配置數據庫 ,固定建立三張表 ,OAuth2 框架須要默認使用這三張表html
我使用的時Mysql,工具爲navcatjava
CREATE TABLE `oauth_access_token` ( `token_id` varchar(256) DEFAULT NULL, `token` blob, `authentication_id` varchar(250) NOT NULL, `user_name` varchar(256) DEFAULT NULL, `client_id` varchar(256) DEFAULT NULL, `authentication` blob, `refresh_token` varchar(256) DEFAULT NULL, PRIMARY KEY (`authentication_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ---------- CREATE TABLE `oauth_client_details` ( `client_id` varchar(250) NOT NULL, `resource_ids` varchar(256) DEFAULT NULL, `client_secret` varchar(256) DEFAULT NULL, `scope` varchar(256) DEFAULT NULL, `authorized_grant_types` varchar(256) DEFAULT NULL, `web_server_redirect_uri` varchar(256) DEFAULT NULL, `authorities` varchar(256) DEFAULT NULL, `access_token_validity` int(11) DEFAULT NULL, `refresh_token_validity` int(11) DEFAULT NULL, `additional_information` varchar(4096) DEFAULT NULL, `autoapprove` varchar(256) DEFAULT NULL, PRIMARY KEY (`client_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ---------- CREATE TABLE `oauth_refresh_token` ( `token_id` varchar(256) DEFAULT NULL, `token` blob, `authentication` blob ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
第二步:IDEA+maven+springboot 集成OAuth2
這是服務器工程目錄git
1.pom中配置web
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>OauthText</artifactId> <groupId>OauthText</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>OAuthService</artifactId> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencies> <!-- Spring Security Oauth2 --> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> </dependencies> </project>
2.進行AuthorizationServerConfiguration.java 進行基本配置spring
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { @Autowired private DataSource dataSource; @Bean // 聲明TokenStore實現 public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean // 聲明 ClientDetails實現 public ClientDetailsService clientDetails() { return new JdbcClientDetailsService(dataSource); } @Autowired private TokenStore tokenStore; @Autowired private AuthenticationManager authenticationManager; @Autowired private UserService userService; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients .jdbc(dataSource); } @Override // 配置框架應用上述實現 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); endpoints.tokenStore(tokenStore()); endpoints.userDetailsService(userService); // 配置TokenServices參數 DefaultTokenServices tokenServices = new DefaultTokenServices(); tokenServices.setTokenStore(endpoints.getTokenStore()); tokenServices.setSupportRefreshToken(true); tokenServices.setClientDetailsService(endpoints.getClientDetailsService()); tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer()); tokenServices.setAccessTokenValiditySeconds( (int) TimeUnit.DAYS.toSeconds(1)); // 1天 endpoints.tokenServices(tokenServices); } @Bean @Primary public DefaultTokenServices tokenServices() { DefaultTokenServices tokenServices = new DefaultTokenServices(); tokenServices.setSupportRefreshToken(true); // support refresh token tokenServices.setTokenStore(tokenStore); // use jdbc token store return tokenServices; }
3.ResourceServerConfiguration.java 進行配置sql
@Configuration @EnableResourceServer public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/**").authenticated() .anyRequest().authenticated(); } }
4.WebSecurityConfiguration.java 進行配置數據庫
@Configuration public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter { private final UserService userService; @Autowired public WebSecurityConfiguration(UserService userService) { this.userService = userService; } @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService); } }
5.進行自定義攔截配置
在service接口中apache
service 簡單實現代碼springboot
@Service public class UserServiceImpl implements UserService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println("===================獲取到token已進入自定義驗證:"+username); // 能夠進行數據庫請求,這裏進行模擬 User user = new User(); user.setUsername("110"); user.setPassword("110119"); if (user == null) { System.out.println("==================="+username); throw new UsernameNotFoundException("Could not find the user '" + username + "'"); } return new CustomUserDetails(user, true, true, true, true, null); } }
6.CustomUserDetails OAuth2 提供方法進行配置服務器
public class CustomUserDetails extends User implements UserDetails { private static final long serialVersionUID = 1702923242319850756L; private final boolean enabled; private final boolean accountNonExpired; private final boolean credentialsNonExpired; private final boolean accountNonLocked; private final Set<GrantedAuthority> authorities; public CustomUserDetails(User user, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) { if (user != null && !StringUtils.isBlank(user.getUsername()) && !StringUtils.isBlank(user.getPassword())) { setUsername(user.getUsername()); setPassword(user.getPassword()); this.enabled = enabled; this.accountNonExpired = accountNonExpired; this.credentialsNonExpired = credentialsNonExpired; this.accountNonLocked = accountNonLocked; this.authorities = Collections.unmodifiableSet(new HashSet<>(CollectionUtils.emptyIfNull(authorities))); } else { throw new IllegalArgumentException("Cannot pass null or empty values to constructor"); } } public static long getSerialVersionUID() { return serialVersionUID; } @Override public boolean isEnabled() { return enabled; } @Override public boolean isAccountNonExpired() { return accountNonExpired; } @Override public boolean isCredentialsNonExpired() { return credentialsNonExpired; } @Override public boolean isAccountNonLocked() { return accountNonLocked; } @Override public Set<GrantedAuthority> getAuthorities() { return authorities; } }
這是Controller 配置只要訪問log 就會被OAuth2 進行攔截驗證
@RestController @RequestMapping("/") public class TextController { @PostMapping(value = "/log") public String saveCuringEvidence(@RequestBody User user){ System.out.println("---------------------user:"+user.getUsername()); return user.getUsername(); } }
第三部:好了接下來進行postmain工具 模擬進行請求 url爲你的域名+/oauth/token 獲取token
1.首先配置客戶端密碼
2.而username和password 須要在剛剛創建的數據庫進行自定義配置
3.模仿form表單進行配置
4.上面的帳戶和密碼是自定義驗證時須要的信息要和service裏的帳戶和密碼一致
成功以後
獲取的access_token,複製下來
5.模仿客戶端根據token請求數據
成功請求到數據 收工。。。。
OAuth2.0 配套客戶端實現 :http://www.cnblogs.com/memoryXudy/p/7805217.html
源碼:https://gitee.com/xdymemory00/Security-OAuth2-MiMaMoShi.git