Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
ruoyi-bs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
过峰
ruoyi-bs
Commits
1a560350
提交
1a560350
authored
12月 13, 2024
作者:
陈柏江
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
LoginNew 添加匿名访问权限
上级
c34f92d1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
40 行增加
和
44 行删除
+40
-44
SecurityConfig.java
.../main/java/com/ruoyi/framework/config/SecurityConfig.java
+40
-44
没有找到文件。
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
浏览文件 @
1a560350
package
com
.
ruoyi
.
framework
.
config
;
package
com
.
ruoyi
.
framework
.
config
;
import
com.ruoyi.framework.config.properties.PermitAllUrlProperties
;
import
com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter
;
import
com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl
;
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.context.annotation.Configuration
;
...
@@ -16,26 +20,21 @@ import org.springframework.security.web.SecurityFilterChain;
...
@@ -16,26 +20,21 @@ import org.springframework.security.web.SecurityFilterChain;
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.security.web.authentication.logout.LogoutFilter
;
import
org.springframework.web.filter.CorsFilter
;
import
org.springframework.web.filter.CorsFilter
;
import
com.ruoyi.framework.config.properties.PermitAllUrlProperties
;
import
com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter
;
import
com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl
;
import
com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl
;
/**
/**
* spring security配置
* spring security配置
*
*
* @author ruoyi
* @author ruoyi
*/
*/
@EnableMethodSecurity
(
prePostEnabled
=
true
,
securedEnabled
=
true
)
@EnableMethodSecurity
(
prePostEnabled
=
true
,
securedEnabled
=
true
)
@Configuration
@Configuration
public
class
SecurityConfig
public
class
SecurityConfig
{
{
/**
/**
* 自定义用户认证逻辑
* 自定义用户认证逻辑
*/
*/
@Autowired
@Autowired
private
UserDetailsService
userDetailsService
;
private
UserDetailsService
userDetailsService
;
/**
/**
* 认证失败处理类
* 认证失败处理类
*/
*/
...
@@ -53,7 +52,7 @@ public class SecurityConfig
...
@@ -53,7 +52,7 @@ public class SecurityConfig
*/
*/
@Autowired
@Autowired
private
JwtAuthenticationTokenFilter
authenticationTokenFilter
;
private
JwtAuthenticationTokenFilter
authenticationTokenFilter
;
/**
/**
* 跨域过滤器
* 跨域过滤器
*/
*/
...
@@ -70,8 +69,7 @@ public class SecurityConfig
...
@@ -70,8 +69,7 @@ public class SecurityConfig
* 身份验证实现
* 身份验证实现
*/
*/
@Bean
@Bean
public
AuthenticationManager
authenticationManager
()
public
AuthenticationManager
authenticationManager
()
{
{
DaoAuthenticationProvider
daoAuthenticationProvider
=
new
DaoAuthenticationProvider
();
DaoAuthenticationProvider
daoAuthenticationProvider
=
new
DaoAuthenticationProvider
();
daoAuthenticationProvider
.
setUserDetailsService
(
userDetailsService
);
daoAuthenticationProvider
.
setUserDetailsService
(
userDetailsService
);
daoAuthenticationProvider
.
setPasswordEncoder
(
bCryptPasswordEncoder
());
daoAuthenticationProvider
.
setPasswordEncoder
(
bCryptPasswordEncoder
());
...
@@ -94,46 +92,44 @@ public class SecurityConfig
...
@@ -94,46 +92,44 @@ public class SecurityConfig
* authenticated | 用户登录后可访问
* authenticated | 用户登录后可访问
*/
*/
@Bean
@Bean
protected
SecurityFilterChain
filterChain
(
HttpSecurity
httpSecurity
)
throws
Exception
protected
SecurityFilterChain
filterChain
(
HttpSecurity
httpSecurity
)
throws
Exception
{
{
return
httpSecurity
return
httpSecurity
// CSRF禁用,因为不使用session
// CSRF禁用,因为不使用session
.
csrf
(
csrf
->
csrf
.
disable
())
.
csrf
(
csrf
->
csrf
.
disable
())
// 禁用HTTP响应标头
// 禁用HTTP响应标头
.
headers
((
headersCustomizer
)
->
{
.
headers
((
headersCustomizer
)
->
{
headersCustomizer
.
cacheControl
(
cache
->
cache
.
disable
()).
frameOptions
(
options
->
options
.
sameOrigin
());
headersCustomizer
.
cacheControl
(
cache
->
cache
.
disable
()).
frameOptions
(
options
->
options
.
sameOrigin
());
})
})
// 认证失败处理类
// 认证失败处理类
.
exceptionHandling
(
exception
->
exception
.
authenticationEntryPoint
(
unauthorizedHandler
))
.
exceptionHandling
(
exception
->
exception
.
authenticationEntryPoint
(
unauthorizedHandler
))
// 基于token,所以不需要session
// 基于token,所以不需要session
.
sessionManagement
(
session
->
session
.
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
))
.
sessionManagement
(
session
->
session
.
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
))
// 注解标记允许匿名访问的url
// 注解标记允许匿名访问的url
.
authorizeHttpRequests
((
requests
)
->
{
.
authorizeHttpRequests
((
requests
)
->
{
permitAllUrl
.
getUrls
().
forEach
(
url
->
requests
.
antMatchers
(
url
).
permitAll
());
permitAllUrl
.
getUrls
().
forEach
(
url
->
requests
.
antMatchers
(
url
).
permitAll
());
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
requests
.
antMatchers
(
"/login
"
,
"/register"
,
"/captchaImage"
).
permitAll
()
requests
.
antMatchers
(
"/login"
,
"/loginNew
"
,
"/register"
,
"/captchaImage"
).
permitAll
()
// 静态资源,可匿名访问
// 静态资源,可匿名访问
.
antMatchers
(
HttpMethod
.
GET
,
"/"
,
"/*.html"
,
"/**/*.html"
,
"/**/*.css"
,
"/**/*.js"
,
"/profile/**"
).
permitAll
()
.
antMatchers
(
HttpMethod
.
GET
,
"/"
,
"/*.html"
,
"/**/*.html"
,
"/**/*.css"
,
"/**/*.js"
,
"/profile/**"
).
permitAll
()
.
antMatchers
(
"/swagger-ui.html"
,
"/swagger-resources/**"
,
"/webjars/**"
,
"/*/api-docs"
,
"/druid/**"
).
permitAll
()
.
antMatchers
(
"/swagger-ui.html"
,
"/swagger-resources/**"
,
"/webjars/**"
,
"/*/api-docs"
,
"/druid/**"
).
permitAll
()
// 除上面外的所有请求全部需要鉴权认证
// 除上面外的所有请求全部需要鉴权认证
.
anyRequest
().
authenticated
();
.
anyRequest
().
authenticated
();
})
})
// 添加Logout filter
// 添加Logout filter
.
logout
(
logout
->
logout
.
logoutUrl
(
"/logout"
).
logoutSuccessHandler
(
logoutSuccessHandler
))
.
logout
(
logout
->
logout
.
logoutUrl
(
"/logout"
).
logoutSuccessHandler
(
logoutSuccessHandler
))
// 添加JWT filter
// 添加JWT filter
.
addFilterBefore
(
authenticationTokenFilter
,
UsernamePasswordAuthenticationFilter
.
class
)
.
addFilterBefore
(
authenticationTokenFilter
,
UsernamePasswordAuthenticationFilter
.
class
)
// 添加CORS filter
// 添加CORS filter
.
addFilterBefore
(
corsFilter
,
JwtAuthenticationTokenFilter
.
class
)
.
addFilterBefore
(
corsFilter
,
JwtAuthenticationTokenFilter
.
class
)
.
addFilterBefore
(
corsFilter
,
LogoutFilter
.
class
)
.
addFilterBefore
(
corsFilter
,
LogoutFilter
.
class
)
.
build
();
.
build
();
}
}
/**
/**
* 强散列哈希加密实现
* 强散列哈希加密实现
*/
*/
@Bean
@Bean
public
BCryptPasswordEncoder
bCryptPasswordEncoder
()
public
BCryptPasswordEncoder
bCryptPasswordEncoder
()
{
{
return
new
BCryptPasswordEncoder
();
return
new
BCryptPasswordEncoder
();
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论