Spring security OAuth2.0认证授权学习第四天(SpringBoot集成)

基础的授权其实只有两行代码就不单独写一个篇章了;

 

这两行就是上一章demo的权限判断;

集成SpringBoot

SpringBoot介绍

 

 

 这个篇章主要是讲SpringSecurity的,SpringBoot不做主要讲解

创建SpringBoot项目

在这里说一下,我的所有项目创建和代码的书写都是使用的IDEA,eclipse我用的不是很明白;

点击File -> new Project

 

 

 我用的是Spring初始化

 

 

 

 配置Maven信息后点击下一步

 

 

 在这里我选择了Lombok.web,和security

 

 

 一些路径的配置,

在POM.xml中我除了选择的又增加了Servlet的依赖

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>

 

springboot工程会在启动时自动扫描启动类所在包中的所有类及其子包子类加载到Spring容器中,所以不再需要SpringConfig.java的配置类

在工程中会有一个application.properties的配置文件,当然也可以换为yml的,我们公司统一使用yml的

在这个类里面配置就可以了

# 端口号
server.port=8080
# 上下文路径
server.servlet.context-path=/abc
# spring 应用程序名
spring.application.name=security

配置SpringWeb的配置类

 1 package com.dance.flower.springbootsecurity.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 6 
 7 /**
 8  * @Description web配置类
 9  * @ClassName SpingWebConfig
10  * @Author mr.zhang
11  * @Date 2020/5/10 23:31
12  * @Version 1.0.0
13  **/
14 @Configuration
15 public class SpingWebConfig implements WebMvcConfigurer {
16 
17     @Override
18     public void addViewControllers(ViewControllerRegistry registry) {
19         registry.addViewController("/").setViewName("redirect:/login");
20     }
21 }

视图解析器也不需要了,也配置在了application.properties中

# 视图前缀
spring.mvc.view.prefix=/WEB-INF/views/
# 视图后缀
spring.mvc.view.suffix=.jsp

springsecurity的配置是一样的,拷贝一下,把注解换为@Configuration

 1 package com.dance.flower.springbootsecurity.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 6 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 7 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 8 import org.springframework.security.core.userdetails.User;
 9 import org.springframework.security.core.userdetails.UserDetailsService;
10 import org.springframework.security.crypto.password.NoOpPasswordEncoder;
11 import org.springframework.security.crypto.password.PasswordEncoder;
12 import org.springframework.security.provisioning.InMemoryUserDetailsManager;
13 
14 /**
15  * @Description 安全配置
16  * @ClassName WebSecurityConfig
17  * @Author mr.zhang
18  * @Date 2020/5/6 17:58
19  * @Version 1.0.0
20  **/
21 @Configuration
22 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
23 
24     /**
25      * 定义用户信息服务(查询用户信息)
26      * @return UserDetailsService
27      */
28     @Bean
29     @Override
30     public UserDetailsService userDetailsService(){
31         // 基于内存比对
32         InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
33         // 创建用户
34         inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("zs").authorities("p1").build());
35         inMemoryUserDetailsManager.createUser(User.withUsername("ls").password("ls").authorities("p2").build());
36         return inMemoryUserDetailsManager;
37     }
38 
39     /**
40      * 密码编码器
41      * @return PasswordEncode
42      */
43     @Bean
44     public PasswordEncoder passwordEncoder(){
45         // 暂时采用字符串比对
46         return NoOpPasswordEncoder.getInstance();
47     }
48 
49     /**
50      * 安全拦截机制
51      * @param http
52      * @throws Exception
53      */
54     @Override
55     protected void configure(HttpSecurity http) throws Exception {
56         // 认证请求
57         http.authorizeRequests()
58                 .antMatchers("/r/r1").hasAuthority("p1")
59                 .antMatchers("/r/r2").hasAuthority("p2")
60                 // 需要认证
61                 .antMatchers("/r/**").authenticated()
62                 // 其他的放行
63                 .anyRequest().permitAll()
64                 // 并且
65                 .and()
66                 // 允许表单登录
67                 .formLogin()
68                 // 成功后转发地址
69                 .successForwardUrl("/success");
70     }
71 }

Controller直接拷贝

 1 package com.dance.flower.springbootsecurity.controller;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 /**
 7  * @Description 认证控制器
 8  * @ClassName AuthService
 9  * @Author mr.zhang
10  * @Date 2020/5/2 17:40
11  * @Version 1.0.0
12  **/
13 @RestController
14 public class AuthController {
15 
16     @RequestMapping(value = "/r/r1",produces = {"application/json;charset=UTF-8"})
17     public String r1(){
18         return "访问资源r1";
19     }
20 
21     @RequestMapping(value = "/r/r2",produces = {"application/json;charset=UTF-8"})
22     public String r2(){
23         return "访问资源r2";
24     }
25 
26     /**
27      * 成功后跳转 提供给SpringSecurity使用
28      * @return
29      */
30     @RequestMapping(value="/success",produces = ("text/plain;charset=UTF-8"))
31     public String loginSuccess(){
32         return "登录成功";
33     }
34 
35 
36 }

开始启动.....  运行Main方法

我就知道我的启动不会是一帆风顺的,好吧,报错了,检查之后发现,还是自己作的原因,为啥要在POM.xml中添加Servlet依赖呢,自己也很迷糊,注释掉了,重新启动成功

 

 不用在意8080后的那个名字,本来是abc,在我排查错误的时候,以为是context-path和application.name不一致的问题,后来重试了一下,发现不是,,所以不用在意

 

用户名密码还是之前配置的么有变

洗洗睡了

作者:彼岸舞

时间:2020\05\10

内容关于:spring security

本文部分来源于网络,只做技术分享,一概不负任何责任

 

posted @ 2020-05-10 23:56  彼岸舞  阅读(507)  评论(9编辑  收藏  举报