auth2
This commit is contained in:
1 parent
d35402cc6a
commit
6ac13817a4
7 files changed
+171
No files matched your search
@@ -0,0 +1,17 @@
|
|||||||
|
package com.ruoyi;
|
||||||
|
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class BCryptMain {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BCryptPasswordEncoder encoder =
|
||||||
|
new BCryptPasswordEncoder();
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
encoder.encode("123456")
|
||||||
|
);
|
||||||
|
System.out.println(new String(Base64.getEncoder().encode("lu7213:123456".getBytes())));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.ruoyi.web.controller.openapi;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http://127.0.0.1:2020/oauth/token
|
||||||
|
* Basic Auth lu7213/123456
|
||||||
|
* x-www-form-urlencoded
|
||||||
|
* grant_type:client_credentials
|
||||||
|
* scope:openapi
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/openapi/test")
|
||||||
|
public class OpenApiController {
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String test(){
|
||||||
|
return "oauth2 ok";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,6 +84,13 @@
|
|||||||
<version>3.4.0</version>
|
<version>3.4.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Security OAuth2 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
|
<version>2.2.13.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OAuth2 授权服务器配置
|
||||||
|
* 支持 client_credentials 模式
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableAuthorizationServer
|
||||||
|
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BCryptPasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置客户端详情服务
|
||||||
|
* client_credentials 模式不需要用户认证,只需要客户端认证
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
|
||||||
|
clients.inMemory()
|
||||||
|
.withClient("lu7213") // 客户端ID
|
||||||
|
.secret(passwordEncoder.encode("123456")) // 客户端密钥(加密)
|
||||||
|
.authorizedGrantTypes("client_credentials") // 授权类型:client_credentials
|
||||||
|
.scopes("openapi") // 授权范围
|
||||||
|
.accessTokenValiditySeconds(3600) // token有效期(秒)
|
||||||
|
.and()
|
||||||
|
.withClient("luqj-client") // 额外的客户端
|
||||||
|
.secret(passwordEncoder.encode("luqj-secret"))
|
||||||
|
.authorizedGrantTypes("client_credentials")
|
||||||
|
.scopes("openapi", "api")
|
||||||
|
.accessTokenValiditySeconds(7200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置授权服务器端点
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
|
||||||
|
endpoints
|
||||||
|
.authenticationManager(authenticationManager);
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||||
|
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OAuth2 资源服务器配置
|
||||||
|
* 保护 openapi/** 路径的资源
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableResourceServer
|
||||||
|
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
|
||||||
|
|
||||||
|
private static final String OPENAPI_RESOURCE_ID = "openapi-resource";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置资源服务器安全属性
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
|
||||||
|
resources.resourceId(OPENAPI_RESOURCE_ID).stateless(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置资源服务器的 HTTP 安全
|
||||||
|
* openapi/** 路径需要 OAuth2 token 认证
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void configure(HttpSecurity http) throws Exception {
|
||||||
|
http
|
||||||
|
.requestMatchers()
|
||||||
|
.antMatchers("/openapi/**")
|
||||||
|
.and()
|
||||||
|
.authorizeRequests()
|
||||||
|
.antMatchers("/openapi/**")
|
||||||
|
.access("#oauth2.hasScope('openapi')")
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
.and()
|
||||||
|
.sessionManagement()
|
||||||
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
|
.and()
|
||||||
|
.csrf().disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
|
|||||||
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
@@ -19,6 +20,8 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
|
|||||||
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -28,6 +31,8 @@ import java.util.List;
|
|||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
||||||
|
@Configuration
|
||||||
|
@Order(101) // OAuth2 ResourceServer order=3, WebSecurity order需要更高,避免冲突
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
/**
|
/**
|
||||||
* 自定义用户认证逻辑
|
* 自定义用户认证逻辑
|
||||||
|
|||||||
+12
@@ -31,6 +31,18 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
|
|||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||||
throws ServletException, IOException
|
throws ServletException, IOException
|
||||||
{
|
{
|
||||||
|
String uri=request.getRequestURI();
|
||||||
|
|
||||||
|
|
||||||
|
// OAuth2接口不要走JWT
|
||||||
|
if(uri.startsWith("/oauth/")
|
||||||
|
|| uri.startsWith("/openapi/")){
|
||||||
|
chain.doFilter(
|
||||||
|
request,
|
||||||
|
response
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
LoginUser loginUser = tokenService.getLoginUser(request);
|
LoginUser loginUser = tokenService.getLoginUser(request);
|
||||||
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
|
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user