栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-security

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-security

package com.gmy.config;

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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

//aop思想:拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //授权
    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人都可以访问,功能页只有有权限的人才能访问
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()//所有人都可以访问
                .antMatchers("/level1/**").hasRole("vip1")//vip1的可以访问level1
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");


        //没有权限会到登陆页面,需要开启登录页面,定制登录页
        http.formLogin().loginPage("/toLogin");

        //注销功能
        http.csrf().disable();//关闭csrf,
        http.logout().logoutSuccessUrl("/");

        //开启记录我功能 cookie功能 ,默认两周
        http.rememberMe();
    }

    //认证
    //There is no PasswordEncoder mapped for the id "null"
    //在spring secutiry 增加了更多加密方式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常从数据库中取

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("gmy").password(new BCryptPasswordEncoder().encode("135246")).roles("vip1","vip2","vip3")//设置一个用户的权限级别
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("135246")).roles("vip1","vip2")
                .and().
                withUser("ozl").password(new BCryptPasswordEncoder().encode("135246")).roles("vip3");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006718.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号