走進springboot
- 2021 年 4 月 3 日
- 筆記
- springboot
SpringBoot基礎
核心思想—自動裝配—約定大於配置
開發環境:jdk1.8、maven、springboot、idea
一、快速構建一個springboot項目
1.1、進入springboot官網
1.2、選擇配置並下載
1.3、項目的導入
二、自動裝配原理
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</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</artifactId>
<version>2.4.2</version>
</dependency>
<!-- jsr303校驗 後台校驗數據的格式,配合@Validated //數據校驗 ,使用!-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- 自動導入web使用的所有依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot使用yml注入數據必備! -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.1、怎麼進行自動裝配?
spring boot的自動裝配:
1、spring boot啟動時會載入大量的自動配置類
2、在開發中看需要實現的功能是否存在springboot寫好的配置類,如果沒有就需要手動配置
3、在yml中修改springboot屬性值時(也就是配置),當它提示時,也就相當於你在修改springboot配置好的配置類中的屬性
4、springboot配置好的配置類可以在spring.factories中進行查找出來,它會有一個xxxAutoConfiguration(自動配置類:給容器中添加組件),它上面的xxxProperties配置的類屬性就是yml中可以進行修改的屬性,以此來達到想要配置的功能!!!
結論:主啟動器@SpringBootApplication通過掃描依賴中導入的Spring-boot-autoconfigure下的jar包下的META-INF下的spring.factories中的配置進行裝配,但是不一定生效,如果沒有相應的啟動器,就不會生效,需要導入相應的啟動器才能自動裝配,springboot的自動裝配的東西(以前需要自己寫的包或者配置文件xml。。。)現在只需要在sprint-boot-autoconfigure-xxx.xx.RELEASE.jar包下都存在,不需要自己配置,只需要調用即可。。
三、yml寫法
-
yml對空格的要求嚴格
#對象存儲 Dog: name: "lucky" age: "7" Person: name: "阿輝" age : 22 happy: true birth: 2021/2/23 maps: {k1: v1, k2: v2} lists: [code,basketball,girl] dog: name: Lucky age: 22
-
通過@Component和@ConfigurationProperties(prefix = “dog”)將實體類的屬性與yml中的配置進行綁定
-
ConfigurationProperties(prefix = “dog”)必須導入spring-boot-configuration-processor依賴還需要使用prefix進行綁定!
-
package com.example.springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
private String name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
@Validated //數據校驗
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
通過@Autowired的自動裝配原理將實體類中的數據進行輸出
package com.example.springboot;
import com.example.springboot.pojo.Dog;
import com.example.springboot.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private Dog dog;
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(dog);
System.out.println("--------------");
System.out.println(person);
}
}
四、多環境切換測試
application.properties 環境切換(需要三個環境,application.properties、application-test.properties、application-dev.properties)
server.port=8080
# SpringBoot的多環境配置:自主選擇激活配合文件(test/dev)
spring.profiles.active=dev
application.yml方式實現多環境的切換
# 選擇激活哪個環境(test/dev) 各個配置之間拿---分隔
spring:
profiles:
active: test
---
spring:
profiles: test
server:
port: 8082
---
spring:
profiles: dev
server:
port: 8089
五、靜態資源
1、在springboot中,可以使用以下方式處理靜態資源:
-
webjars:
localhost:8080/webjars/
-
public、static、/**、resources 可以使用:“localhost:8080/文件名.後綴名
2、優先順序(如果文件相同則)
resources > static(默認)>public
六、擴展mvc
package com.example.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
//
//@EnableWebMvc就是添加了@Import({DelegatingWebMvcConfiguration.class})這個類,意思就是從容器中獲取所有的webmvcconfig
// 分析:WebMvcAutoConfig類有個@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
// 如果有這個類,那麼MyConfig就不會自動裝配,
// 又因為上面DelegatingWebMvcConfiguration這個類繼承了WebMvcConfigurationSupport
// 所以不能加@EnableWebMvc這個註解,不然就會失去自動裝配的功能!
// 擴展SpringMVC
@Configuration
public class MyConfig implements WebMvcConfigurer {
// ViewResolver 實現了視圖解析器介面的類 就相當於是個視圖解析器
// 如果想要自定義的功能,只需要把它注入到bean中,交由Springboot,Springboot會幫我們自動裝配!
@Bean // 自定義功能!!!
public ViewResolver myViewResolver(){
return new MyViewResolver();
}
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
// 視圖跳轉
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// addViewController("/springboot")相當於用localhost:8080/springboot這個跳轉到localhost:8080/test
// 相當於更名!!
registry.addViewController("/springboot").setViewName("test");
}
}
七、thymeleaf(模板引擎)的應用
7.1、 作用域的導入
<html xmlns="//www.w3.org/1999/xhtml" xmlns:th="//www.thymeleaf.org">
7.2、thymeleaf的th的應用
(格式:th: href/ img / src=”@{xxx}**”)
<!DOCTYPE html>
<html xmlns="//www.w3.org/1999/xhtml" xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Gurdeep singh osahan">
<meta name="author" content="Gurdeep singh osahan">
<title>Miver</title>
<!-- Favicon Icon -->
<link rel="icon" type="image/png" th:href="@{images/fav.svg}">
<!-- Bootstrap core CSS -->
<link th:href="@{vendor/bootstrap/css/bootstrap.min.css}" rel="stylesheet">
<!-- Font Awesome-->
<link th:href="@{vendor/fontawesome/css/font-awesome.min.css}" rel="stylesheet">
<!-- Material Design Icons -->
<link th:href="@{vendor/icons/css/materialdesignicons.min.css}" media="all" rel="stylesheet" type="text/css">
<!-- Slick -->
<link th:href="@{vendor/slick-master/slick/slick.css}" rel="stylesheet" type="text/css">
<!-- Lightgallery -->
<link th:href="@{vendor/lightgallery-master/dist/css/lightgallery.min.css}" rel="stylesheet">
<!-- Select2 CSS -->
<link th:href="@{vendor/select2/css/select2-bootstrap.css}" />
<link th:href="@{vendor/select2/css/select2.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{css/style.css}" rel="stylesheet">
</head>
<body>
<div class="copyright">
<div class="logo">
<a href="index.html">
<img th:src="@{images/logo.svg}">
</a>
</div>
</div>
<script th:src="@{vendor/jquery/jquery.min.js}"></script>
<script th:src="@{vendor/bootstrap/js/bootstrap.bundle.min.js}"></script>
<!-- Contact form JavaScript -->
<!-- Do not edit these files! In order to set the email address and subject line for the contact form go to the bin/contact_me.php file. -->
<script th:src="@{js/jqBootstrapValidation.js}"></script>
<script th:src="@{js/contact_me.js"></script>
<!-- Slick -->
<script th:src="@{vendor/slick-master/slick/slick.js}" type="text/javascript" charset="utf-8"> </script>
<!-- lightgallery -->
<script th:src="@{vendor/lightgallery-master/dist/js/lightgallery-all.min.js}"></script>
<!-- select2 Js -->
<script th:src="@{vendor/select2/js/select2.min.js}"></script>
<!-- Custom -->
<script th:src="@{js/custom.js}"></script>
</body>
</html>
八、整合Druid(阿里巴巴的資料庫連接池)
8.1、更改yml中資料庫連接type為Druid
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/curry?usrUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
thymeleaf:
cache: false #關閉快取
mode: HTML5 #設置模板類型
encoding: utf-8 #設置編碼
# 列印自動生成的sql語句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
8.2、實現Druid綁定到yml中
並實現一個後台監控功能
package com.example.springboot_data.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DruidConfig {
//綁定到yml中
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
@Bean
//後台監控功能 :spring可以在增加web功能下的web.xml中配置 但是springboot就通過ServletRegistrationBean配置
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//後台登錄 賬戶密碼配置
HashMap<String,String> initParameters = new HashMap<>();
//賬戶密碼設置
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","admin");
// 禁止誰訪問!
initParameters.put("ahui","192.168.11.12");
bean.setInitParameters(initParameters);
System.out.println(bean.getInitParameters()+"this is getInit");
return bean;
}
}
8.3、實現filter過濾器
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DruidConfig {
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
bean.setFilter(new WebStatFilter());
// 過濾請求
HashMap<String, String> initParameters = new HashMap<>();
//過濾這些東西
initParameters.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParameters);
return bean;
}
}
九、springboot整合mybatis
9.1、UserController實現跳轉
package com.example.springboot_mybatis.controller;
import com.example.springboot_mybatis.mapper.UserMapper;
import com.example.springboot_mybatis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired(required = false)
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> users = userMapper.queryUserList();
users.forEach(System.out::println);
return users;
}
// 使用restful風格取值id 並查詢
@GetMapping("/queryById/{id}")
public User queryById( @PathVariable Integer id){
return userMapper.queryById(id);
}
}
9.2、UserMapper介面實現查詢功能
package com.example.springboot_mybatis.mapper;
import com.example.springboot_mybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper // @Mapper表示這是一個mybatis的mapper類
// 也可以在主程式入口使用@MapperScan("com.example.springboot_mybatis.mapper"))
@Repository
//@Component
public interface UserMapper {
@Select("select * from user")
List<User> queryUserList();
@Select("select * from user where id = #{id}")
User queryById(@Param("id") int id);
int addUser(@Param("User")User user);
int updateUser(@Param("User")User user);
int deleteUserById(@Param("id")int id);
}
9.3、User實體類
package com.example.springboot_mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
private String email;
private Integer version;
private String gmt_create;
private String gmt_modified;
}
9.4、yml配置連接資料庫及綁定mybatis
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
username: root
password: root
url: jdbc:mysql://localhost:3306/curry?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
#整合mybatis
mybatis:
type-aliases-package: com.example.springboot_mybaits.pojo
# mapper-locations: classpath:mybatis/mapper/*.xml 用註解就直接省去xml配置
十、SpringSecurity安全機制
安全框架:shiro、springsecurity
安全框架的作用:認證、授權
- 功能許可權
- 訪問許可權
- 菜單許可權
重要security類:
webSecurityConfiguration
: 自定義 security 策略AuthenticationManagerBuilder
: 自定義認真策略@EnableWebSecurity
: 開啟 WebSecurity 模式
配置類:SecurityConfig.java
package com.example.spring_security.config;
import com.example.spring_security.controller.RouterController;
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.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")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3"); //認證請求
// 沒有許可權,進入就需要登錄
http.formLogin();
//開啟註銷功能 並跳轉到首頁
http.logout().logoutSuccessUrl("/");
// springSecurity為了防止網站攻擊 默認開啟了csrf功能
// http.csrf().disable();
}
// 認證 springboot 2.1.x 可以直接使用
// 密碼編碼: PasswordEncoder 沒有編碼的錯誤~!
// 如果沒有密碼編碼伺服器會報500錯誤 :.withUser("guest").password("guest").roles("vip1");
// 對他進行加密之後:new BCryptPasswordEncoder().encode("curry")
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("ahui").password(new BCryptPasswordEncoder().encode("curry")).roles("vip2","vip3")
.and()
.withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip2","vip3","vip1")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("guest")).roles("vip1");
}
}
controller類:RouterController.java
package com.example.spring_security.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class RouterController {
@RequestMapping("/index")
public String toIndex(){
return "index";
}
@RequestMapping("/login")
public String toLogin(){
return "/views/login";
}
@PostMapping("/user/login")
public String redirectLogin(Model model, HttpSession httpSession, @RequestParam("username") String userName, @RequestParam("password") String passWord){
if (!StringUtils.isEmpty(userName) && "admin".equals(passWord))
{
// httpSession.setAttribute("loginUser",userName);
return "redirect:/index.html";
}
else
{
model.addAttribute("msg","用戶名或密碼錯誤");
return "views/login";
}
}
@RequestMapping("/level1/{id}")
public String toLevel1(@PathVariable("id") int id){
return "views/level1/"+id;
}
@RequestMapping("/level2/{id}")
public String toLevel2(@PathVariable("id") int id){
return "views/level2/"+id;
}
@RequestMapping("/level3/{id}")
public String toLevel3(@PathVariable("id") int id){
return "views/level3/"+id;
}
}
十一、shiro安全機制
shiro需要一個config類來實現過濾 和一個realm對象來實現認證與授權
application.yml
#連接資料庫的配置,以及使用druid數據源進行連接
spring:
thymeleaf:
cache: false
datasource:
url: jdbc:mysql://localhost:3306/curry?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
# mapper-locations: classpath:mapper/*.xml //使用mybatis註解實現,不需要使用xml方式
type-aliases-package: com.example.shiro_springboot.pojo
shiroconfig.java
package com.example.shiro_springboot.config;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
// shiroFilterFactoryBean 過濾器
@Bean // 通過@Qualifier("defaultWebSecurityManager")與下面的@Bean(name = "defaultWebSecurityManager")的方法綁定
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
// 設置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
//添加shiro的內置過濾器!
/*
* anon : 無需認證就可以訪問
* authc : 必須認證了才能訪問
* user : 必須擁有記住我功能才能訪問
* perms : 擁有對某個資源的許可權才能訪問
* role : 擁有某個角色許可權才能訪問
* */
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
// filterChainDefinitionMap.put("/user/add","authc");
// filterChainDefinitionMap.put("/user/update","authc");
//許可權設置 沒有add許可權
filterChainDefinitionMap.put("/user/update","perms[user:update]");
filterChainDefinitionMap.put("/user/add","perms[user:add]");
filterChainDefinitionMap.put("/user/*","authc");
// 授權跳轉
bean.setUnauthorizedUrl("/noauth");
bean.setLoginUrl("/toLogin");
// 設置攔截器
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}
// DefaultWebSecurityManager
@Bean(name = "defaultWebSecurityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
// 通過@Qualifier("userRealm")與下面的UserRealm的方法綁定
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 關聯UserRealm
securityManager.setRealm(userRealm);
return securityManager;
}
//創建realm 對象
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
Userrealm.java
package com.example.shiro_springboot.config;
import com.example.shiro_springboot.pojo.User;
import com.example.shiro_springboot.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
public class UserRealm extends AuthorizingRealm {
@Autowired
UserService userService;
// 授權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("執行了授權方法");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermission("user:add");
//拿到當前登錄的用戶 的對象
Subject subject = SecurityUtils.getSubject();
User currentUser = (User) subject.getPrincipal(); //拿到User對象
//
info.addStringPermission(currentUser.getPerms());
return info;
}
// 認證
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("執行了"+authenticationToken+"方法!!!");
//獲取當前的用戶
Subject subject = SecurityUtils.getSubject();
//偽造的資料庫資訊
// String name = "admin";
// String password = "admin";
UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
//連接真實資料庫
User user = userService.queryUserByName(userToken.getUsername());
if(user == null) // 如果等於null 表示沒有這個人
return null;
//Shiro 做密碼加密方式,Md5 md5鹽支加密
// shiro做的密碼認證 直接交給shiro
return new SimpleAuthenticationInfo("",user.getPwd(),"");
}
}
跳轉視圖
ShiroController.java
package com.example.shiro_springboot.controller;
import com.example.shiro_springboot.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ShiroController {
@Autowired(required = false)
private UserMapper userMapper;
@GetMapping({"/","/index"})
public String sayShiro(Model model){
model.addAttribute("msg","hello shiro!!!");
return "index";
}
// @GetMapping("/userList")
// public String userList(Model model){
// List<User> users = userMapper.queryUserByName();
// model.addAttribute("msg",users);
//
// users.forEach(System.out::println);
// return "user/showUsers";
// }
@GetMapping("/user/add")
public String add(){
return "user/add";
}
@GetMapping("/user/update")
public String update(){
return "user/update";
}
@GetMapping("/toLogin")
public String toLogin(){
return "user/login";
}
@RequestMapping("/noauth")
@ResponseBody
public String noAuth(){
return "未經授權無法登陸!";
}
@RequestMapping("/login")
public String login(String username, String password, Model model){
// 獲取當前用戶
Subject subject = SecurityUtils.getSubject();
// 封裝用戶的登錄數據
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token); // 執行登錄的方法,如果沒有異常,就登錄成功!
return "index";
}catch (UnknownAccountException e){
model.addAttribute("msg","用戶名錯誤!");
return "user/login";
}
catch (IncorrectCredentialsException e){
model.addAttribute("msg","密碼錯誤!");
return "user/login";
}
}
}
UserMapper.java 查詢資料庫
package com.example.shiro_springboot.mapper;
import com.example.shiro_springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
//@Component
public interface UserMapper {
@Select("select * from user where name = #{name}")
public User queryUserByName(String name);
}
實體類pojo/User.java
package com.example.shiro_springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String perms;
private String name;
private String pwd;
private Integer age;
private String email;
private Integer version;
private Date gmt_create;
private Date gmt_modified;
}
十二、Swagger
-
前後端分離——技術棧 (Vue + Springboot)
- Swagger版本3.0.x 就不支援 訪問localhost:8080/swagger-ui.html介面訪問到
- 而Swagger的2.9.x則支援
-
swagger依賴
<!-- swagger依賴--> <!-- //mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.1</version> </dependency> <!-- //mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.1</version> </dependency>
-
SwaggerConfig配置
package com.example.springboot_swagger.config; import io.swagger.annotations.Api; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration @EnableSwagger2 //開啟Swagger public class SwaggerConfig { //配置了Swagger的Bean實例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // .enable(true) true表示啟動swagger false表示不啟動swagger .select() // basePackage 表示指定掃描哪個包 .paths(PathSelectors.ant("/example")) // 過濾路徑! .build(); // build工廠模式 } // 配置Swagger資訊 ApiInfo類 private ApiInfo apiInfo(){ return new ApiInfo("阿輝的Swagger", "Api Documentation", "v1.0", "urn:tos", new Contact("阿輝", "//www.baidu.com", "[email protected]"), "Apache 2.0", "//www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
-
swagger 實現頂部分組功能!
- 不同分組實現不同環境的介面
@Bean public Docket docket(){ // 顯示swagger環境 Profiles profiles = Profiles.of("dev","test"); System.out.println(profiles+"dasdas"); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // 如何做到多個分組 多個docket實例就可以做到多個分組 及多個docket方法!方法名不同即可 .groupName("阿輝") .select() // basePackage 表示指定掃描哪個包 .paths(PathSelectors.ant("/example")) // 過濾路徑! .build(); // build工廠模式 } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("Stephen"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2).groupName("curry"); }
十三、非同步任務實現
service層實現多執行緒模擬
package com.example.demo.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class TaskService {
@Async // 表示為非同步鎖!
public void ok() throws InterruptedException {
Thread.sleep(3000);
System.out.println("數據正在處理中……");
}
}
controller層實現
- Controller調用service層
package com.example.demo.controller;
import com.example.demo.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TaskController {
@Autowired
TaskService taskService;
@RequestMapping("/hello")
@ResponseBody
public String sayHello() throws InterruptedException {
taskService.ok();
return "OK";
}
}
-
實現非同步任務調度 還需要在主類上開啟非同步
@EnableAsync
-
定時任務的執行
corn表達式的執行
-
需要在主類上加入定時任務執行的註解
@EnableScheduling //開啟定時任務
-
需要在被執行的方法的上面加上
@Scheduled(cron = "0/5 * * * * ?")
package com.example.demo.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class TaskService {
//corn表達式 corn表達式依次順序 秒 分 時 日 月 星期(可以設置0-7或者? 表示每天)
// corn = "30 0/5 10,18 * ?" 表示每天的十點和十八點 每隔五分鐘執行一次
//corn = "0 0 12 ? * 1-6" 每個月的周一到周六12點00分執行一次
@Scheduled(cron = "0/5 * * * * ?") //每天每時每刻五秒執行一次
public void hello(){
System.out.println("hello 被執行!");
}
}
十四、序列化的實現
springboot集成redis ,redis存儲對象需要使用到序列化方式去傳遞!
package com.example.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all") // 消除所有的警告問題
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 公司序列化的模板!!!!
// 為了開發方便,一般使用<String, Object>
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//String的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//Key採用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash的key也採用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化方式採用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化方式採用Jackson
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
把redis常用操作集中一起 定義成一個組件
- RedisUtil
package com.example.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired //自動裝配我們上面所配置的序列化方式
private RedisTemplate<String, Object> redisTemplate;
// =============================common============================
/**
* 指定快取失效時間
* @param key 鍵
* @param time 時間(秒)
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據key 獲取過期時間
* @param key 鍵 不能為null
* @return 時間(秒) 返回0代表為永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判斷key是否存在
* @param key 鍵
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除快取
* @param key 可以傳一個值 或多個
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
}
}
}
// ============================String=============================
/**
* 普通快取獲取
* @param key 鍵
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通快取放入
* @param key 鍵
* @param value 值
* @return true成功 false失敗
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通快取放入並設置時間
* @param key 鍵
* @param value 值
* @param time 時間(秒) time要大於0 如果time小於等於0 將設置無限期
* @return true成功 false 失敗
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 遞增
* @param key 鍵
* @param delta 要增加幾(大於0)
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞增因子必須大於0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 遞減
* @param key 鍵
* @param delta 要減少幾(小於0)
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞減因子必須大於0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Map=================================
/**
* HashGet
* @param key 鍵 不能為null
* @param item 項 不能為null
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 獲取hashKey對應的所有鍵值
* @param key 鍵
* @return 對應的多個鍵值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
* @param key 鍵
* @param map 對應多個鍵值
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 並設置時間
* @param key 鍵
* @param map 對應多個鍵值
* @param time 時間(秒)
* @return true成功 false失敗
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張hash表中放入數據,如果不存在將創建
*
* @param key 鍵
* @param item 項
* @param value 值
* @return true 成功 false失敗
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張hash表中放入數據,如果不存在將創建
*
* @param key 鍵
* @param item 項
* @param value 值
* @param time 時間(秒) 注意:如果已存在的hash表有時間,這裡將會替換原有的時間
* @return true 成功 false失敗
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除hash表中的值
*
* @param key 鍵 不能為null
* @param item 項 可以使多個 不能為null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判斷hash表中是否有該項的值
*
* @param key 鍵 不能為null
* @param item 項 不能為null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash遞增 如果不存在,就會創建一個 並把新增後的值返回
*
* @param key 鍵
* @param item 項
* @param by 要增加幾(大於0)
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash遞減
*
* @param key 鍵
* @param item 項
* @param by 要減少記(小於0)
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================set=============================
/**
* 根據key獲取Set中的所有值
* @param key 鍵
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根據value從一個set中查詢,是否存在
*
* @param key 鍵
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將數據放入set快取
*
* @param key 鍵
* @param values 值 可以是多個
* @return 成功個數
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 將set數據放入快取
*
* @param key 鍵
* @param time 時間(秒)
* @param values 值 可以是多個
* @return 成功個數
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 獲取set快取的長度
*
* @param key 鍵
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值為value的
*
* @param key 鍵
* @param values 值 可以是多個
* @return 移除的個數
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================list=================================
/**
* 獲取list快取的內容
* @param key 鍵
* @param start 開始
* @param end 結束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取list快取的長度
*
* @param key 鍵
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通過索引 獲取list中的值
*
* @param key 鍵
* @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數第二個元素,依次類推
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 將list放入快取
*
* @param key 鍵
* @param value 值
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入快取
* @param key 鍵
* @param value 值
* @param time 時間(秒)
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入快取
*
* @param key 鍵
* @param value 值
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入快取
*
* @param key 鍵
* @param value 值
* @param time 時間(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據索引修改list中的某條數據
*
* @param key 鍵
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N個值為value
*
* @param key 鍵
* @param count 移除多少個
* @param value 值
* @return 移除的個數
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}