微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot学习(五) -高级

目录

一.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

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简单整合

现在模拟一下购票流程,一个用户模块,一个购票模块。当用户调用购票功能时,会远程调用购票模块的购票功能

在这里插入图片描述

两个模块都需要导入pom依赖

      <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,启动即可

@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();
    }
}

现在运行hello(),调用的tickerService.buy()就是远程的生产者的方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐