目录
一.安全
1. 登录&认证&授权
里面写了文字详解哦~~~~
package com.ys.springbootsecurity.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;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
//ctrl+o 重写方法 定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasAnyRole("VIP1")
.antMatchers("/level2/**").hasAnyRole("VIP2");
//开启自动配置的登录功能,如果没登录,没有权限就会跳到登录页面
http.formLogin();
/*
* 1.login来到登录页
* 2.重定向到/login?error表示登录失败
* 3.更多详细规定 看文档 源码
*
* */
}
//定制认证规则 就是用户(它是存在内存中的,也可以连接数据库)
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication()
.withUser("zhangsan").password("{noop}123456").roles("VIP1","VIP2");
}
}
2. 权限控制与注销
全部代码
package com.ys.springbootsecurity.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;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
//ctrl+o 重写方法 定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasAnyRole("VIP1")
.antMatchers("/level2/**").hasAnyRole("VIP2")
.antMatchers("/level3/**").hasAnyRole("VIP3");
//开启自动配置的登录功能,如果没登录,没有权限就会跳到登录页面
http.formLogin();
/*
* 1.login来到登录页
* 2.重定向到/login?error表示登录失败
* 3.更多详细规定 看文档 源码
*
* */
//开启自动配置的注销
http.logout().logoutSuccessUrl("/");
/*
* 1. 访问/logout表示用户注销 清空session
* 2. 注销成功会返回/login?logout 页面,登录页面
* 然后我可以修改跳转到指定页面 .logoutSuccessUrl()
*
*
* */
}
//定制认证规则 就是用户(它是存在内存中的,也可以连接数据库)
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication()
.withUser("zhangsan").password("{noop}123456").roles("VIP1","VIP2");
}
}
开启自动配置的注销
http.logout().logoutSuccessUrl("/");
/*
* 1. 访问/logout表示用户注销 清空session
* 2. 注销成功会返回/login?logout 页面,登录页面
* 然后我可以修改跳转到指定页面 .logoutSuccessUrl()
*
*
* */
然后导入这个依赖
然后在xml网页里设置权限显示
首先要 这个
引用security的名称空间<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<div sec:authorize="!isAuthenticated()"></div>
这个是如果用户没认证 那么就显示div内的东西
所以就有
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
如果他认证了 那么就显示下面的东西 身份 以及注销按钮
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好!您的身份有:
<span sec:authentication="principal.authorities"></span>
</h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销">
</form>
</div>
然后主要的内容显示与否 看他的身份!!!
例如 第一个文档只能 是VIP1才能访问所以加上
<div sec:authorize="hasRole('VIP1')">
</div>
这个是判别用户是VIP1 才会显示div内容
所以网页的代码如下!!!
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好!您的身份有:
<span sec:authentication="principal.authorities"></span>
</h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销">
</form>
</div>
<hr>
<div sec:authorize="hasRole('VIP1')">
<h3>普通武功秘籍</h3>
<ul>
<li><a th:href="@{/level1/1}">罗汉拳</a></li>
<li><a th:href="@{/level1/2}">武当长拳</a></li>
<li><a th:href="@{/level1/3}">全真剑法</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP2')">
<h3>高级武功秘籍</h3>
<ul>
<li><a th:href="@{/level2/1}">太极拳</a></li>
<li><a th:href="@{/level2/2}">七伤拳</a></li>
<li><a th:href="@{/level2/3}">梯云纵</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP3')">
<h3>绝世武功秘籍</h3>
<ul>
<li><a th:href="@{/level3/1}">葵花宝典</a></li>
<li><a th:href="@{/level3/2}">龟派气功</a></li>
<li><a th:href="@{/level3/3}">独孤九剑</a></li>
</ul>
</div>
</body>
</html>
3.记住用户并自动登录
同上面一样只需要加上这一句就OK了!
即使退出下次进入页面 也会自动登录。它是用的cookie
4. 定制页面+定制记住用户
不用security定制的登录页面用自己的定制页面
首先 跳转到自己定制的页面 所以要修改这个
在userlogin页面 改为
welcome页面改为!
记住用户定制
传递一个参数 然后
在登录页面加上标签
参数相互对应
二.springboot与分布式
1.简介
2. docker安装zookeeper
首先 下载
3. springboot、dubbo、zookeeper整合
最后有个bug没跑起来 唉。。。
4.springboot与spring clude
建空模板:
provider-ticket模板
consumer-user 模板
eureka-service 是服务中心
在其下面
写入 :
运行成功后访问 localhost:8761端口就可以了
5.分布式-服务注册
首先在提供者里提供服务
里面写一个方法返回string 记得@Service
再建一个
然后启动应用 访问localhost:8001/ticket 这个是与mapping对应的
那么就显示对应的String内容
在注册中心中可以看到状态
6.服务发现与消费
在consumer-user内建
三. springboot与热部署
就是我的这个程序
我得在页面输入/hello才可以访问
假设我修改为/
我ctrl +f9(Build project)后 可以不用启动程序 直接去网页输入就可以了
xml文件也同理