spring security入门程序----登录

spring security
spring security是一款安全访问控制解决方案的安全框架,就是限制没有登录的用户和没有权限的用户不能访问某些页面,也就是常说的权限控制。
spring security官网
http://projects.spring.io/spring-security/

配置文件
开发环境是 spring boot ,maven。spring security版本4.2.3 ,spring boot 版本1.5.4

先在maven里配置下载,配置文件内容官网上有maven和gradle版本的,我这里只放出我的。

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

因为是在spring boot上集成的,其它的配置在java类里声明。
新建一个WebSecurityConfig继承WebSecurityConfigurerAdapter,添加
@EnableWebSecurity注释,重写configure方法。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .csrf().disable()     //关闭csrf
              .authorizeRequests()        //权限控制
              .antMatchers("/", "/home").permitAll()       //允许所有用户访问
              .antMatchers("/js/**","/css/**","/image/**").permitAll()
              .antMatchers("/account").hasRole("USER")       //允许"USER"用户访问
              .antMatchers("/index").hasRole("USER")
              .anyRequest().authenticated()       //其他所有都要登录验证
          .and()
              .rememberMe()       //开启cookie
              .tokenValiditySeconds(3600)       //cookie有效期,单位:秒
              .key("myKey")       //cookie私钥
           .and()
           .formLogin()       //登录
                .loginPage("/login")       //登录页面
                .defaultSuccessUrl("/userhome")       //登录成功跳转页面
                .failureUrl("/loginError?error=1")       //失败调用接口,也可以是页面
                .permitAll()     
           .and()
           .logout()       //退出登录
                 .logoutSuccessUrl("/login")       //退出登录成功跳转页面
                 .permitAll();
    }
}

由于csrf的配置比较繁琐,和主题无关,先关掉了。
接着是重写configureGlobal。


 @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
              .inMemoryAuthentication()
             .withUser("123").password("123").roles("USER");
    }

这里是直接使用的内存中的账号来验证,.inMemoryAuthentication()使用内存中的用户 .withUser(“123”).password(“123”)设置用户的账号和密码,.roles(“USER”)设置用户权限,当然也可以用数据库中的账号来验证。
授权的配置
.antMatchers() 使用ant风格的路径匹配
.regexMatchers() 用正则匹配
.anyRequest() 匹配所有请求路径

匹配路径后接着赋予权限,常用的有
.hasAnyRoles(“USER”,”ADMIN”) “USER”和”ADMIN”可以访问
.hasRole(“USER”) “USER”可以访问
.hasIpAddress(“192...“)** 特定ip可以访问
.permitAll() 允许所有用户访问
.authenticated() 允许用户登录后访问

html页面
html页面内容比较简单,security已经完成了登录和退出的接口,登录只需要post方法将 username和password传递到”/login”即可,退出登录分两种情况,如果是开启了csrf,需要post传值,如果没有,只需要get调用”/logout”即可。
登录

<form th:action="@{/login}" method="post" >
            <div >
                <label > 用户名 : 
                    <input type="text" name="username" /> 
                </label>
            </div>
            <div >
                <label> 密码: 
                    <input type="password" name="password" /> 
                </label>
            </div>
            <div><input type="submit" value="登录"/></div>

退出登录

<a href="/logout" method = "get" >安全退出</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值