欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringBoot學習(二)——springboot快速整合springsecurity組件

Spring Security

簡介

spring security的核心功能為認證(Authentication),授權(quán)(Authorization),即認證用戶是否能訪問該系統(tǒng),和授權(quán)用戶可以在系統(tǒng)中進行哪些操作。

創(chuàng)新互聯(lián)建站是專業(yè)的同心網(wǎng)站建設(shè)公司,同心接單;提供網(wǎng)站設(shè)計、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行同心網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

引入spring security組件

在 pom.xml 中加入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
</dependency>

驗證組件是否起到作用,現(xiàn)在不更改框架內(nèi)的任何內(nèi)容,啟動項目,瀏覽器中依舊輸入 http://localhost:8080 ,可看到如下界面,之前可以直接進入spring boot的初始界面,現(xiàn)在已經(jīng)看不見了,spring security 導入后默認已經(jīng)開啟了驗證,必須先登錄驗證通過后才能訪問。

SpringBoot學習(二)—— springboot快速整合spring security組件

如果代碼中不做任何設(shè)置,默認的賬戶是 user,默認的密碼隨著項目的啟動,會打印在控制臺中。
SpringBoot學習(二)—— springboot快速整合spring security組件

輸入賬號密碼,即可進入默認的初始界面。
SpringBoot學習(二)—— springboot快速整合spring security組件

代碼實戰(zhàn)

為了最快最簡單最直接的認識這個組件,直接把用戶密碼寫入內(nèi)存中,項目啟動即存在,避免還有建表,實體類,數(shù)據(jù)庫操作等與之無關(guān)的內(nèi)容。命名使用最為簡單粗暴的方式,排除一切干擾,用最少的精力掌握該組件的使用。

新增代碼目錄
SpringBoot學習(二)—— springboot快速整合spring security組件

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    SPRING BOOT !!!
</body>
</html>

error.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    錯誤
</body>
</html>

UserController

package com.example.controller;

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

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("/addUser")
    @ResponseBody
    String addUser() {
        return "這是添加用戶?。?!";
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    String deleteUser() {
        return "這是刪除用戶?。?!";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    String updateUser() {
        return "這是修改用戶!??!";
    }

    @RequestMapping("/findAllUsers")
    @ResponseBody
    String findAllUsers() {
        return "這是查詢用戶!??!";
    }

}

UserSecurityConfig

package com.example.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;

//注解開啟 Spring Security 安全認證與授權(quán)
@EnableWebSecurity
public class UserSecurityConfig extends WebSecurityConfigurerAdapter {

    //用戶認證
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //內(nèi)存里面放著
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
                //添加用戶,密碼,角色
                .withUser("zs").password("123456").roles("AAA")
                //鏈式編程
                .and()
                .withUser("ls").password("123456").roles("BBB")
                .and()
                .withUser("ww").password("123456").roles("CCC", "primary")
                .and()
                .withUser("zl").password("123456").roles("primary");
    }

    //用戶授權(quán)
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * permitAll():允許一切用戶訪問
         * hasRole():url請求允許訪問的角色
         * hasAnyRole() : url請求允許訪問的多個角色
         * access():允許訪問的角色,permitAll、hasRole、hasAnyRole 底層都是調(diào)用 access 方法
         * access("permitAll") 等價于 permitAll()
         */
        http.authorizeRequests().antMatchers("/").permitAll(); // "/":應用首頁所以用戶都可以訪問
        http.authorizeRequests()
                .antMatchers("/user/addUser").hasRole("AAA") // 首斜杠"/"表示應用上下文,/user/addUser 請求允許 AAA 角色訪問
                .antMatchers("/user/deleteUser/**").hasAnyRole("AAA", "BBB") //"/user/deleteUser/**"允許 "AAA", "BBB" 角色訪問,/**匹配任意
                .antMatchers("/user/updateUser").hasAnyRole("AAA", "BBB", "CCC")//除了這種鏈式編程,也可以分開寫
                .antMatchers("/user/findAllUsers").access("permitAll");

        http.authorizeRequests().anyRequest().authenticated();

        /**
         * formLogin:指定支持基于表單的身份驗證
         * 當用戶沒有登錄、沒有權(quán)限時就會自動跳轉(zhuǎn)到登錄頁面(默認 /login)
         * 當?shù)卿浭r,默認跳轉(zhuǎn)到 /error
         * 登錄成功時會放行
         */
        http.formLogin();
    }

}

MyPasswordEncoder

package com.example.config;

import org.springframework.security.crypto.password.PasswordEncoder;

//密碼編碼,Spring Security 高版本必須進行密碼編碼,否則報錯
public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }
}

親測效果是

以用戶名 zs 登錄(其角色權(quán)限為AAA),可以進入系統(tǒng),瀏覽器輸入地址可以訪問, localhost:8080,localhost:8080/user/addUser,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers

以用戶名 ls 登錄(其角色權(quán)限為BBB),可以進入系統(tǒng),瀏覽器輸入地址可以訪問, localhost:8080,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers

以用戶名 ww 登錄(其角色權(quán)限為CCC),可以進入系統(tǒng),瀏覽器輸入地址可以訪問, localhost:8080,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers

以用戶名 zl 登錄(其角色權(quán)限為CCC),可以進入系統(tǒng),瀏覽器輸入地址可以訪問, localhost:8080,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers

以用戶名 admin 登錄,不可以進入系統(tǒng),因為系統(tǒng)中還沒有該用戶。

分享名稱:SpringBoot學習(二)——springboot快速整合springsecurity組件
標題路徑:http://www.aaarwkj.com/article6/gjciog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化App開發(fā)、網(wǎng)站制作品牌網(wǎng)站建設(shè)、標簽優(yōu)化、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
熟妇高潮一区二区在线观看| 日本在线最新视频一区二区三区| 国产精品99久久久久久人| 欧美三级黄片免费视频| 欧美国产日韩一区在线观看| 麻豆精品情欲人妻二区| 国产精品黄色片在线观看 | 亚洲一级香蕉视频东京热| 亚洲精品不卡一区二区| av在线免费观看不卡| 国产精品一区午夜福利| 亚洲精品欧美综合二区| 99精品国产一区二区青青性色| 91大片在线观看视频| 久久婷婷欧美激情综合| 国产亚洲一区二区三区乱码| 中文字幕日韩精品久久| 国产区精品福利在线熟女| 欧美日韩免费r在线视频| 91免费观看视频高清| 内射极品美女在线观看| 激情五月开心五月麻豆| 午夜精品视频免费91| 免费黄色福利网址大片| 欧美亚洲综合日韩精品区| 成人黄色小视频下载| 欧美精品蜜桃激情一区久久| 熟妇丰满多毛的大阴户| 日本不卡的三区四区五区| 亚洲老熟女老妇老女人| 亚洲午夜一区二区三区精品影院| 丰满熟妇久久人妻同堂av| 日本在线不卡二区三区| 一二三日韩电影在线观看| 人妻上司无奈中文字幕| 欧美日韩精品国产精品| 欧美成人高清在线播放| 91熟女成人精品一区二区| 国产一级性生活高清在线| 中文字幕制服日韩久久一区| 97成人在线免费视频|