目录
一.SpringBoot与安全
1.Spring Security
引入Spring Security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
编写配置类
@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");
//开启自动配置的登录功能
//访问 /login 来到登录界面
//如果登录错误,重定向到 /login?error 表示登录失败
//还可以有更多设置,如登录的用户名,密码
http.formLogin();
//开启注销,并且设置注销后返回的地址
//访问/logout 表示用户注销,清空session
//注销成功会返回 /logout:?logout 页面
http.logout().logoutSuccessUrl("/");
//开启 记住我 功能
//登录成功后,将cookie发给浏览器保存,以后登录带上cookie,只要通过检查就可以免登陆
//点击注销后,会销毁这个cookie
http.rememberMe();
}
//定制认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1","VIP2")
.and()
.withUser("wangwu").password("123").roles("VIP1");
}
}
配合thymeleaf模板在前端使用
导入maven依赖
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<Meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--isAuthenticated()是否认证过(登录过) -->
<div sec:authorize="!isAuthenticated()">
<h1>请先登录!</h1>
</div>
<div sec:authorize="isAuthenticated()">
<form th:action="@{/logout}">
<input type="submit" value="注销"></input>
</form>
</div>
<div sec:authorize="hasRole('VIP1')">
<!--sec:authentication="name" 可以查看用户名 -->
<span sec:authentication="name"></span>,你的VIP1用户,你的角色有:
<!-- sec:authentication="principal.authorities" 可以查看权限角色 -->
<span sec:authentication="principal.authorities"></span>
</div>
</body>
</html>
前面都是用的系统给我们的页面,现在我们想用自己的页面怎么办?
只需要在formLogin()后面添加loginPage 访问页面,usernameParameter,passwordParameter 用户名,密码的传递名字
http.formLogin().loginPage("/userlogin").usernameParameter("user").passwordParameter("pwd");
这个时候,记住我 功能也需要自定义
添加rememberMeParameter参数,规定好名字,在前端使用name属性绑定记住我的选项
http.rememberMe().rememberMeParameter("remember");
二.SpringBoot与分布式
1.zookeeper与dubbo
Docker安装zookeeper
下载zookeeper
docker pull zookeeper
查看镜像id
docker images
运行容器
docker run --name zookeepper01 -p 2181:2181 --restart always -d ea93faa92337
开放对应端口的防火墙
firewall-cmd --zone=public --add-port=2181/tcp --permanent
开放后,需要重启一下防火墙
firewall-cmd --reload
如果你的阿里云,还需要去开放一下安全组
springboot简单整合
现在模拟一下购票流程,一个用户模块,一个购票模块。当用户调用购票功能时,会远程调用购票模块的购票功能。
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
生产者
购票模块的Service接口和实现类
public interface TickerService {
void buy();
}
@Service
@Component
public class TickerServiceImpl implements TickerService{
@Override
public void buy() {
System.out.println("买到票了");
}
}
spring.application.name=provider-ticket
#如果指定了spring应用名称,可以缺省dubbo的应用名称,这2个至少要配置1个。缺省dubbo的应用名称时默认值是spring的应用名称
#dubbo.application.name=user-service
dubbo.application.name=provider-ticket
#注册中心地址
dubbo.registry.address=zookeeper://xxx.xxx.xxx:2181
#端口号可以写在address中,也可以单独写。实质是从address中获取的port是null,后面设置的port覆盖了null
#dubbo.registry.port=2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#指定注册到zk上超时时间,ms
dubbo.registry.timeout=10000
#指定实现服务(提供服务)的包
dubbo.scan.base-packages=com.pt.zoodubbo2.service
@Enabledubbo
public class Zoodubbo2Application {
消费者
配置类
spring.application.name=provider-user
#如果指定了spring应用名称,可以缺省dubbo的应用名称,这2个至少要配置1个。缺省dubbo的应用名称时默认值是spring的应用名称
#dubbo.application.name=user-service
dubbo.application.name=provider-ticket
#注册中心地址
dubbo.registry.address=zookeeper://xxx.xxx.xxx:2181
创建一个TickerService接口,包路径和接口内容需要和生产者一致。
public interface TickerService {
void buy();
}
消费者方法,只需要使用@Reference 就可以引入远程的TickerService
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Reference
TickerService tickerService;
public void hello(){
tickerService.buy();
}
}