Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
sts网站
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyang
sts网站
Commits
1e40e60d
Commit
1e40e60d
authored
Jul 22, 2020
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持CORS跨域请求
parent
133a10ec
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
0 deletions
+37
-0
ResourcesConfig.java
...main/java/com/ruoyi/framework/config/ResourcesConfig.java
+24
-0
SecurityConfig.java
.../main/java/com/ruoyi/framework/config/SecurityConfig.java
+11
-0
GenController.java
...in/java/com/ruoyi/generator/controller/GenController.java
+2
-0
No files found.
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java
View file @
1e40e60d
package
com
.
ruoyi
.
framework
.
config
;
package
com
.
ruoyi
.
framework
.
config
;
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.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.cors.UrlBasedCorsConfigurationSource
;
import
org.springframework.web.filter.CorsFilter
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
...
@@ -39,4 +43,24 @@ public class ResourcesConfig implements WebMvcConfigurer
...
@@ -39,4 +43,24 @@ public class ResourcesConfig implements WebMvcConfigurer
{
{
registry
.
addInterceptor
(
repeatSubmitInterceptor
).
addPathPatterns
(
"/**"
);
registry
.
addInterceptor
(
repeatSubmitInterceptor
).
addPathPatterns
(
"/**"
);
}
}
/**
* 跨域配置
*/
@Bean
public
CorsFilter
corsFilter
()
{
UrlBasedCorsConfigurationSource
source
=
new
UrlBasedCorsConfigurationSource
();
CorsConfiguration
config
=
new
CorsConfiguration
();
config
.
setAllowCredentials
(
true
);
// 设置访问源地址
config
.
addAllowedOrigin
(
"*"
);
// 设置访问源请求头
config
.
addAllowedHeader
(
"*"
);
// 设置访问源请求方法
config
.
addAllowedMethod
(
"*"
);
// 对接口配置跨域设置
source
.
registerCorsConfiguration
(
"/**"
,
config
);
return
new
CorsFilter
(
source
);
}
}
}
\ No newline at end of file
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
View file @
1e40e60d
...
@@ -12,6 +12,8 @@ import org.springframework.security.config.http.SessionCreationPolicy;
...
@@ -12,6 +12,8 @@ import org.springframework.security.config.http.SessionCreationPolicy;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
;
import
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
;
import
org.springframework.security.web.authentication.logout.LogoutFilter
;
import
org.springframework.web.filter.CorsFilter
;
import
com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter
;
import
com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter
;
import
com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl
;
import
com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl
;
import
com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl
;
import
com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl
;
...
@@ -47,6 +49,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
...
@@ -47,6 +49,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
*/
*/
@Autowired
@Autowired
private
JwtAuthenticationTokenFilter
authenticationTokenFilter
;
private
JwtAuthenticationTokenFilter
authenticationTokenFilter
;
/**
* 跨域过滤器
*/
@Autowired
private
CorsFilter
corsFilter
;
/**
/**
* 解决 无法直接注入 AuthenticationManager
* 解决 无法直接注入 AuthenticationManager
...
@@ -112,6 +120,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
...
@@ -112,6 +120,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
httpSecurity
.
logout
().
logoutUrl
(
"/logout"
).
logoutSuccessHandler
(
logoutSuccessHandler
);
httpSecurity
.
logout
().
logoutUrl
(
"/logout"
).
logoutSuccessHandler
(
logoutSuccessHandler
);
// 添加JWT filter
// 添加JWT filter
httpSecurity
.
addFilterBefore
(
authenticationTokenFilter
,
UsernamePasswordAuthenticationFilter
.
class
);
httpSecurity
.
addFilterBefore
(
authenticationTokenFilter
,
UsernamePasswordAuthenticationFilter
.
class
);
// 添加CORS filter
httpSecurity
.
addFilterBefore
(
corsFilter
,
JwtAuthenticationTokenFilter
.
class
);
httpSecurity
.
addFilterBefore
(
corsFilter
,
LogoutFilter
.
class
);
}
}
...
...
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
View file @
1e40e60d
...
@@ -178,6 +178,8 @@ public class GenController extends BaseController
...
@@ -178,6 +178,8 @@ public class GenController extends BaseController
private
void
genCode
(
HttpServletResponse
response
,
byte
[]
data
)
throws
IOException
private
void
genCode
(
HttpServletResponse
response
,
byte
[]
data
)
throws
IOException
{
{
response
.
reset
();
response
.
reset
();
response
.
addHeader
(
"Access-Control-Allow-Origin"
,
"*"
);
response
.
addHeader
(
"Access-Control-Expose-Headers"
,
"Content-Disposition"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\"ruoyi.zip\""
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\"ruoyi.zip\""
);
response
.
addHeader
(
"Content-Length"
,
""
+
data
.
length
);
response
.
addHeader
(
"Content-Length"
,
""
+
data
.
length
);
response
.
setContentType
(
"application/octet-stream; charset=UTF-8"
);
response
.
setContentType
(
"application/octet-stream; charset=UTF-8"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment