亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關于SpringSecurity配置403權限訪問頁面的完整代碼

 更新時間:2021年06月19日 14:47:34   作者:別團等shy哥發(fā)育  
本文給大家分享SpringSecurity配置403權限訪問頁面的完整代碼,配置之前和配置之后的詳細介紹,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

1、未配置之前

在這里插入圖片描述

2、開始配置

 2.1 新建一個unauth.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>沒有訪問的權限</h1>
</body>
</html>

2.2 在繼承WebSecurityConfigurerAdapter的配置類中設置

關鍵代碼:

//配置沒有權限訪問自定義跳轉的頁面
  http.exceptionHandling()
  .accessDeniedPage("/unauth.html");

配置類完整代碼:

package com.atguigu.springsecuritydemo1.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password(){
       return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //退出配置
        http.logout().logoutUrl("/logout")
                .logoutSuccessUrl("/test/hello")
                .permitAll();

        //配置沒有權限訪問自定義跳轉的頁面
        http.exceptionHandling().accessDeniedPage("/unauth.html");
        http.formLogin()             //自定義自己編寫的登陸頁面
            .loginPage("/login.html")    //登錄頁面設置
            .loginProcessingUrl("/user/login") //登錄訪問路徑
            .defaultSuccessUrl("/success.html").permitAll()    //登錄成功之后,跳轉路徑
            .and().authorizeRequests()
               //設置哪些路徑可以直接訪問,不需要認證
                .antMatchers("/","/test/hello","/user/login").permitAll()
                //當前登錄的用戶,只有具有admins權限才可以訪問這個路徑
               //1、hasAuthority方法
               //.antMatchers("/test/index").hasAuthority("admins")
               //2、hasAnyAuthority方法
              // .antMatchers("/test/index").hasAnyAuthority("admins,manager")
              //3、hasRole方法  ROLE_sale
               .antMatchers("/test/index").hasRole("sale")
                //4、hasAnyRole方法

            .anyRequest().authenticated()
            .and().csrf().disable();    //關閉csrf防護
    }
}

2.3 繼承UserDetailsService接口的實現(xiàn)類

package com.atguigu.springsecuritydemo1.service;

import com.atguigu.springsecuritydemo1.entity.Users;
import com.atguigu.springsecuritydemo1.mapper.UsersMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //調用userMapper中的方法,根據用戶名查詢數據庫
        QueryWrapper<Users> wrapper=new QueryWrapper<>();//條件構造器
        //where username=?
        wrapper.eq("username",username);
        Users users= usersMapper.selectOne(wrapper);
        //判斷
        if(users==null){    //數據庫沒有用戶名,認證失敗
            throw new UsernameNotFoundException("用戶名不存在!");
        }

        List<GrantedAuthority> auths= AuthorityUtils.commaSeparatedStringToAuthorityList("admins,ROLE_sale");
        //從查詢數據庫返回user對象,得到用戶名和密碼,返回
        return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);
    }

}

3、測試

現(xiàn)在我故意將原先的sale改為sale1制造錯誤

在這里插入圖片描述

啟動項目并訪問http://localhost:8111/test/index

在這里插入圖片描述

輸入lucy 123

在這里插入圖片描述

成功實現(xiàn)

以上就是SpringSecurity配置403權限訪問頁面的詳細內容,更多關于SpringSecurity權限訪問頁面的資料請關注腳本之家其它相關文章!

相關文章

  • Java獲取網絡文件并插入數據庫的代碼

    Java獲取網絡文件并插入數據庫的代碼

    抓取各大網站的數據插入數據庫,這樣就不用為沒有數據而煩惱了
    2010-06-06
  • 如何用Dos命令運行Java版HelloWorld你知道嗎

    如何用Dos命令運行Java版HelloWorld你知道嗎

    這篇文章主要介紹了在dos窗口中編譯和運行java文件的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 淺談Spring Session工作原理

    淺談Spring Session工作原理

    Spring Session是為了解決多進程session共享的問題,本文將介紹怎么使用Spring Session,以及Spring Session工作原理
    2021-06-06
  • IDEA找不到jdk該如何解決

    IDEA找不到jdk該如何解決

    這篇文章主要給大家介紹了關于IDEA找不到jdk該如何解決的相關資料,剛安裝好IDEA后,我們運行一個項目時候,有時候會遇到顯示找不到Java的JDK,需要的朋友可以參考下
    2023-11-11
  • eclipse部署tomcat服務器無法啟動問題的解決方法

    eclipse部署tomcat服務器無法啟動問題的解決方法

    這篇文章主要為大家詳細介紹了eclipse部署tomcat服務器無法啟動問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • springcloud?eureka切換nacos的配置方法

    springcloud?eureka切換nacos的配置方法

    這篇文章主要介紹了springcloud?eureka切換nacos,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • SpringSecurity注銷設置的方法

    SpringSecurity注銷設置的方法

    這篇文章主要為大家詳細介紹了SpringSecurity注銷設置的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Java連接MySQL8.0 JDBC的詳細步驟(IDEA版本)

    Java連接MySQL8.0 JDBC的詳細步驟(IDEA版本)

    這篇文章主要介紹了Java連接MySQL8.0 JDBC的詳細步驟(IDEA版本),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Spingboot?JPA?CriteriaBuilder?如何獲取指定字段

    Spingboot?JPA?CriteriaBuilder?如何獲取指定字段

    這篇文章?主要介紹了Spingboot?JPA?CriteriaBuilder?如何獲取指定字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Cloud Gateway 默認的filter功能和執(zhí)行順序介紹

    Spring Cloud Gateway 默認的filter功能和執(zhí)行順序介紹

    這篇文章主要介紹了Spring Cloud Gateway 默認的filter功能和執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論