springSecurity入门demo

与shiro一样,springSecurity也是一个安全框架,核心也是认证和授权,本篇博客将使用springBoot做一个springSecurity的入门demo,完整代码已放到了GitHub:https://github.com/qiuxinfa/springSecurity-study   实现如下功能:

(1)自定义基于内存的用户名和密码

(2)自定义表单登录

1.新建maven工程,添加如下依赖:

    <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.创建一个controller,以便测试:

@RestController
public class HelloSecurityController {

    @GetMapping("/hello")
    public String hello (){
        return "hello spring security";
    }
}

   这时,创建一个启动类,启动项目,访问 http://localhost:8080/hello ,发现无法访问,跳到了如下的登录页:

默认的用户名是user,密码是控制台打印出来的一串UUID,注意,每次启动的密码都不一样:

这说明,接口已经被springSecurity保护起来了。

 

3.自定义配置类,继承WebSecurityConfigurerAdapter,解释都在代码中了:

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter{

    @Bean
    public PasswordEncoder passwordEncoder(){
        //暂时不加密,要加密的话,可以return new BCryptPasswordEncoder();

        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //放行静态资源
        web.ignoring().antMatchers("/js/**", "/css/**","/images/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()  //允许基于使用HttpServletRequest限制访问
                //所有请求都需要认证
                .anyRequest().authenticated()
                .and()
                //表单登录
                .formLogin()
                //登录页面和处理接口
                .loginPage("/login.html")
                .permitAll()
                .and()
                //关闭跨站请求伪造的防护,这里是为了前期开发方便
                .csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("123").roles("admin","user")
                .and()
                .withUser("qxf")
                .password("123").roles("user");

    }
}

这里,自定义了2个用户,一个是admin,一个是qxf,密码都是123,暂时没有加密,这里有一个坑,自定义的用户一定要加角色信息,不然会报错,详细错误,点击这里

 

4.自定义登录页:

div class="container">
        <form class="form-signin" method="post" action="/login.html">
            <img src="images/1.jpg">
            <p>
                <label>用户名:</label>
                <input type="text" name="username" class="form-control" placeholder="Username">
            </p>
            <p>
                <label>密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
                <input type="password" name="password" class="form-control" placeholder="Password">
            </p>
            <button class="btn" type="submit">登录</button>
        </form>
    </div>

然后启动项目:发现已经进入到了自定义的登录页面了

 

此时用admin/123或者qxf/123登录即可访问

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值