spring security环境框架搭建

2021-06-04 06:04

阅读:666

YPE html>

标签:example   rmi   html   man   configure   thymeleaf   put   contex   dex   

1,pom依赖导入

注:spring boot版本须使用2。1.0以下版本,此次使用2.0.9版本

org.springframework.boot
            spring-boot-starter-security
org.thymeleaf.extras
            thymeleaf-extras-springsecurity4
            3.0.4.RELEASE

2,编写login.html




登录

登录

注册


Spring Security

3,首页,index.html






首页


Spring Security Study by 秦疆


Level 1

Level-1-1
Level-1-2
Level-1-3
Level 2

Level-2-1
Level-2-2
Level-2-3
Level 3

Level-3-1
Level-3-2
Level-3-3

4,页面跳转controller

package com.example.springsecurity.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RouterController {

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "views/login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id){
        return "views/level1/"+id;
    }
    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id){
        return "views/level2/"+id;
    }
    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id){
        return "views/v/"+id;
    }
}

5,securityConfig配置类编写

package com.example.springsecurity.config;

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.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("v1")
                .antMatchers("/level2/**").hasRole("v2")
                .antMatchers("/level3/**").hasRole("v3");

        //没有权限会默认到登录页,需要开启登陆的页面   /login
        //usernameParameter,passwordParameter:自定义前端接受参数
        http.formLogin().loginPage("/toLogin").usernameParameter("").passwordParameter("");

        //注销,跳到首页
        http.csrf().disable();//关闭csrf,登陆失败的原因
        http.logout().logoutSuccessUrl("/");

        //自定义前端remember接受参数
        http.rememberMe().rememberMeParameter("");
    }

    //认证
    //密码编码:PasswordEncoder
    //在spring security 5.0+新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //数据正常应该从数据库中读
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("v2","v3")
                .and()
                .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("v1","v2","v3");
    }
}

spring security环境框架搭建

标签:example   rmi   html   man   configure   thymeleaf   put   contex   dex   

原文地址:https://www.cnblogs.com/huang580256/p/14657404.html


评论


亲,登录后才可以留言!