Spring Security入门一

中文文档

1.新建项目

	在idea中新建一个springboot项目引入security依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

2.Spring Security初体验

新建一个测试Controller

@RestController
@RequestMapping
public class TestController {
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!!!";
    }
}

在浏览器访问接口 /hello 时会重定向登录界面
登陆界面
输入用户名 user 密码为项目启动时控制台输出的
密码
结果
在这里插入图片描述

3. 配置 用户名/密码

配置文件配置

spring:
  security:
    user:
      name: user
      password: 123

java代码配置内存用户

一: InMemoryUserDetailsManager方式

@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService()   {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        //在内存中创建一个user用户
        manager.createUser(User.withUsername("user").password("123456").roles("USER").build());
        return manager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        //return new BCryptPasswordEncoder();
        return NoOpPasswordEncoder.getInstance();
    }

}

二: AuthenticationManagerBuilder 进行实现

@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /**
         * 如果需要配置多个用户,用 and 相连
         *
         *
         */

        auth.inMemoryAuthentication()
                .withUser("user")
                .password("1234567").roles("admin");
    }

    /**
     * 这两种创建内存用户同时存在 userDetailsService 无效
     * @return
     */
    /*@Bean
    public UserDetailsService userDetailsService()   {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        //在内存中创建一个user用户
        manager.createUser(User.withUsername("user").password("123456").roles("USER").build());
        return manager;
    }*/

    @Bean
    public PasswordEncoder passwordEncoder() {
     //  目前简单案例不进行加密
        return NoOpPasswordEncoder.getInstance();
    }
}

对用户请求开启授权管理

我们的示例仅要求对用户进行身份验证,并且对应用程序中的每个 URL 都进行了身份验证。我们可以通过将多个子级添加到http.authorizeRequests()方法中来为 URL 指定自定义要求。例如:

@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {



    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/hello").hasRole("admin")
                .anyRequest().authenticated()
                .and()
                .formLogin();

    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /**
         * 如果需要配置多个用户,用 and 相连
         *
         *
         */

        auth.inMemoryAuthentication()
                .withUser("user")
                .password("1234567").roles("admin").and()
                .withUser("user2").password("1234567").roles("user");
    }

    /**
     * 这两种创建内存用户同时存在 userDetailsService 无效
     * @return
     */
    /*@Bean
    public UserDetailsService userDetailsService()   {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        //在内存中创建一个user用户
        manager.createUser(User.withUsername("user").password("123456").roles("USER").build());
        return manager;
    }*/

    @Bean
    public PasswordEncoder passwordEncoder() {
     //  目前简单案例不进行加密
        return NoOpPasswordEncoder.getInstance();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值