實現權限控制步驟如下:
**1. 配置訪問權限**
```java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/to/login")
.loginProcessingUrl("/login")
.successForwardUrl("/success")
.failureForwardUrl("/fail");
http.authorizeRequests()
.antMatchers("/layui/**", "/to/login")
.permitAll()
//當用戶有admin權限時才能訪問/account/list01
.antMatchers("/account/list01").hasAuthority("admin")
//當用戶有admin02,或admin03權限時才能訪問/account/list02
.antMatchers("/account/list02").hasAnyAuthority("admin02", "admin03")
//當用戶屬于role角色時才能訪問/account/list03,否則出現403頁面
.antMatchers("/account/list03").hasRole("role")
//當用戶屬于role02,或role03角色時才能訪問
.antMatchers("/account/list04").hasAnyRole("role02", "role03")
.anyRequest()
.authenticated();
http.csrf().disable();
}
}
```
**2. 在用戶登錄時加載當前用戶所有的權限與角色**
```java
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements UserDetailsService {
final AccountService accountService;
final MenuMapper menuMapper;
final RoleMapper roleMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountService.findByUsername(username);
if (account == null) {
throw new UsernameNotFoundException("用戶名不存在!");
}
//從數據庫中查詢當前用戶的權限與角色
List<Menu> menuList = menuMapper.findByAccountId(account.getId());
List<Role> roleList = roleMapper.findByAccountId(account.getId());
//存儲權限與角色的集合
List<GrantedAuthority> authorities = new ArrayList<>(1);
//處理權限
for (Menu menu : menuList) {
authorities.add(new SimpleGrantedAuthority(menu.getPermission()));
}
//處理角色
for (Role role : roleList) {
//角色必須以 ROLE_ 字符串為前綴
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
}
return new User(username, account.getPassword(), authorities);
}
}
```
**3. 測試**
登錄之后,有權限訪問的則正常訪問,沒有權限訪問的返回403頁面。
```
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jun 10 20:21:37 CST 2022
There was an unexpected error (type=Forbidden, status=403).
Forbidden
```
- 跨域問題
- 跨域是什么
- 跨域解決方案
- 從后端解決
- nginx反向代理
- WebSocket
- websocket是什么
- websocket協議
- 使用場景
- 實現方式
- 注解與html5原生方式
- websocketAPI
- 實現步驟
- 文件上傳
- 文件下載
- 廣播通信
- 定時推送
- 編程與socketjs方式
- socketjs與stompjs框架
- 實現步驟
- 重載目的地
- SimpMessagingTemplate
- 定時向前端推送數據
- 5種監聽事件
- 點對點通信
- 攔截器
- HandshakeInterceptor
- ChannelInterceptor
- poi之excel表格
- 表格版本
- POI常用類
- POI依賴
- 寫表格
- 編寫表格過程
- 單元格邊框樣式
- 單元格背景色
- 凍結行或列
- 單元格合并
- 單元格內換行
- 文檔內跳轉
- 讀表格
- Web中的Excel操作
- 導出表格
- 讀取表格
- poi之word文檔
- word版本
- 寫word
- 基本使用
- 標題樣式
- 添加圖片
- EasyExcel表格
- EasyExcel是什么
- 與其他Excel工具對比
- EasyExcel依賴
- 讀Excel
- 簡單讀取
- 指定列位置
- 讀取多個sheet
- 格式轉換
- 多行表頭
- 同步讀
- 寫Excel
- 簡單寫入
- 單元格樣式
- 攔截器
- 列寬
- 凍結行或列
- 合并單元格
- 填充Excel
- SpringSecurity
- SpringSecurity是什么
- 同類型產品對比
- 環境搭建
- 相關概念
- 密碼加密
- Web權限控制
- UserDetailsService接口
- 登錄認證
- 自定義登錄頁
- 未授權跳轉登錄頁
- 權限控制
- 自定義403頁面
- 權限注解
- 記住我功能
- 注銷功能
- CSRF
- CSRF是什么
- CSRF保護演示
- 前后端分離權限控制
- 環境搭建
- 認證實現
- 會話管理
- 動態權限管理
- 微服務權限控制
- 權限控制方案
- SpringBoot整合RabbitMQ
- 整合步驟
- Fanout交換機演示
- Direct交換機演示
- Topic交換機演示
- @RabbitListener方法
- JWT認證與授權
- 環境搭建
- 密碼加密
- 認證與授權