最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于SpringSecurity配置403權(quán)限訪問頁面的完整代碼

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

1、未配置之前

在這里插入圖片描述

2、開始配置

 2.1 新建一個unauth.html

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

2.2 在繼承WebSecurityConfigurerAdapter的配置類中設(shè)置

關(guān)鍵代碼:

//配置沒有權(quán)限訪問自定義跳轉(zhuǎn)的頁面
  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();

        //配置沒有權(quán)限訪問自定義跳轉(zhuǎn)的頁面
        http.exceptionHandling().accessDeniedPage("/unauth.html");
        http.formLogin()             //自定義自己編寫的登陸頁面
            .loginPage("/login.html")    //登錄頁面設(shè)置
            .loginProcessingUrl("/user/login") //登錄訪問路徑
            .defaultSuccessUrl("/success.html").permitAll()    //登錄成功之后,跳轉(zhuǎn)路徑
            .and().authorizeRequests()
               //設(shè)置哪些路徑可以直接訪問,不需要認證
                .antMatchers("/","/test/hello","/user/login").permitAll()
                //當前登錄的用戶,只有具有admins權(quán)限才可以訪問這個路徑
               //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();    //關(guān)閉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 {

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

        List<GrantedAuthority> auths= AuthorityUtils.commaSeparatedStringToAuthorityList("admins,ROLE_sale");
        //從查詢數(shù)據(jù)庫返回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權(quán)限訪問頁面的詳細內(nèi)容,更多關(guān)于SpringSecurity權(quán)限訪問頁面的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • IntelliJ IDEA連接MySQL數(shù)據(jù)庫詳細圖解

    IntelliJ IDEA連接MySQL數(shù)據(jù)庫詳細圖解

    今天小編就為大家分享一篇關(guān)于intellij idea連接mysql數(shù)據(jù)庫詳細圖解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 詳解Hibernate緩存與性能優(yōu)化

    詳解Hibernate緩存與性能優(yōu)化

    在hibernate中,提到性能優(yōu)化,很自然地我們就想到了緩存。緩存是什么,都有哪些呢?下面這篇文章就主要給大家介紹了關(guān)于Hibernate緩存與性能優(yōu)化的相關(guān)資料,需要的朋友可以參考下。
    2017-02-02
  • Java中嵌入式MySQL的使用方法

    Java中嵌入式MySQL的使用方法

    這篇文章主要為大家詳細介紹了Java中嵌入式MySQL的使用方法,Java中如何使用嵌入MySQL,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 15個高級Java多線程面試題及回答

    15個高級Java多線程面試題及回答

    這篇文章主要介紹了15個高級Java多線程面試題及回答,翻譯自國外的一篇文章,這些面試題容易混淆、較難回答,需要的朋友可以參考下吧
    2014-05-05
  • java數(shù)據(jù)結(jié)構(gòu)之棧的詳解

    java數(shù)據(jù)結(jié)構(gòu)之棧的詳解

    這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結(jié)構(gòu)的棧的應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-08-08
  • Java中匿名類的兩種實現(xiàn)方式

    Java中匿名類的兩種實現(xiàn)方式

    本文主要介紹了Java中匿名類的兩種實現(xiàn)方式。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • spring boot hutool整合email的詳細過程

    spring boot hutool整合email的詳細過程

    這篇文章主要介紹了spring boot hutool整合email的相關(guān)知識,本文介紹兩種方式發(fā)送email文件,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • java實現(xiàn)漢字轉(zhuǎn)拼音

    java實現(xiàn)漢字轉(zhuǎn)拼音

    這篇文章主要介紹了java實現(xiàn)漢字轉(zhuǎn)拼音的功能,感興趣的小伙伴們可以參考一下
    2015-12-12
  • 什么是Java布隆過濾器?如何使用你知道嗎

    什么是Java布隆過濾器?如何使用你知道嗎

    這篇文章主要為大家詳細介紹了Java布隆過濾器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 詳細談?wù)凷pring事務(wù)是如何管理的

    詳細談?wù)凷pring事務(wù)是如何管理的

    在使用傳統(tǒng)的事務(wù)編程策略時,程序代碼必然和具體的事務(wù)操作代碼耦合,而使用Spring事務(wù)管理策略恰好可以避免這種尷尬,Spring的事務(wù)管理提供了兩種方式:編程式事務(wù)管理和聲明式事務(wù)管理,這篇文章主要給大家介紹了關(guān)于Spring事務(wù)是如何管理的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論

富民县| 连南| 萨迦县| 上犹县| 宝清县| 山丹县| 古浪县| 遂平县| 交口县| 稷山县| 临桂县| 靖江市| 阳信县| 德兴市| 克什克腾旗| 靖江市| 墨江| 教育| 西吉县| 镇安县| 江永县| 金塔县| 泸溪县| 巴彦县| 科技| 南郑县| 平邑县| 哈密市| 慈利县| 习水县| 桦川县| 绥德县| 石景山区| 丽水市| 莱阳市| 会东县| 五大连池市| 密山市| 海城市| 诸暨市| 珠海市|