二、Springboot+SpringSecurity整合简单权限认证(上篇)

基于springboot下SpringSecurity的简单配置。

一、搭建springboot基本开发环境,我当前项目的pom.xml文件

pom.xml:

  

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cg</groupId>
<artifactId>security_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>security_demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

二、最主要的是springSecurity的配置类

  

package com.cg.config.security;


import com.cg.service.MUserserviceImpl;
import org.aspectj.lang.annotation.Aspect;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

// public UserDetailsService detailsService(){
// return new MUserserviceImpl();
// }
//拦截策略
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("执行了?SecurityConfig+http");
http.authorizeRequests()
.antMatchers("/product/add").hasAuthority("PRO_ADD")
.antMatchers("/product/update").hasAuthority("PRO_UPDATE")
.antMatchers("/product/delete").hasAuthority("PRO_DELETE")
.antMatchers("/product/list").hasAuthority("PRO_LIST")
.antMatchers("/product/login").permitAll()
.antMatchers("/**")
.fullyAuthenticated()
.and()
.formLogin()//.loginPage("/login")
.loginProcessingUrl("/product/add")
.and()
.csrf().disable();
}


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("执行了?SecurityConfig+auth");
auth.inMemoryAuthentication()
.withUser("zhangsan")
.password("{noop}123").authorities("PRO_DELETE","PRO_ADD");

//auth.userDetailsService(detailsService());
}


// @Bean
// public PasswordEncoder passwordEncoder(){
// return new BCryptPasswordEncoder();
// }



}


2、templates模板下的页面html文件

 

 

四、ErrorPage(错误页面配置)

package com.cg.config.security;

import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import org.springframework.boot.web.server.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Configuration
public class ErrorPageConfig2 {

//错误页面配置方式二
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
System.out.println("方式二");
factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,"/product/403"));
}
};
};

}

 五、ProductController 仅是做一个简单跳转

 

package com.cg.mysec.web.product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("product")
public class ProductController {


@RequestMapping("/403")
public String toErrorPage(){
System.out.println("ErrorPage~~~~");
return "error";
}

@RequestMapping("/index")
public String toIndex(String username,String password){
System.out.println("username---"+username);
System.out.println("password---"+password);
System.out.println("index~~~~");
return "index";
}

@RequestMapping("/login")
public String toLogin(){
System.out.println("login");
return "login";
}

@RequestMapping("/add")
public String toAdd(){
System.out.println("product add");
return "add";
}

@RequestMapping("/delete")
public String toDelete(){
System.out.println("product delete");
return "delete";
}

@RequestMapping("/update")
public String toUpdate(){
System.out.println("product update");
return "update";
}

@RequestMapping("/list")
public String toList(){
System.out.println("product list");
return "list";
}

}

六、说明:我自定义登陆界面一直出问题,也没有解决,以后解决了会修改;另外这篇内容只有的用户名和密码并不是从数据库中获取的,该内容将在下篇补充。

转载于:https://www.cnblogs.com/phhblog/p/11156988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值