Spring Secure4在使用上和Secure3差别不大,基本上怎么使用3就可以怎么使用4。而且4也是推荐使用命名空间进行配置,不过由于SpringBoot推荐不使用xml配置,所以我们这里说的都是不使用xml的。sprngboot默认引入的是3,4也类似。
要在项目中通过maven引入spring secure有两种方式,如果使用springboot的starter是这样的:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
也可以直接使用secure的引入方式:
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency>
现在的springboot会把Secure3.2.8引过来。
接下来开始进行配置,只需要新建一个类即可:
@EnableGlobalMethodSecurity(prePostEnabled = true) @Configuration @EnableWebMvcSecurity public class WebAuthConfiguration extends WebSecurityConfigurerAdapter {}
这里我把这个类命名为webauthconfiguration,它需要继承自org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter。
然后给这个类添加Configuration注解,来让springboot启动的时候加载该配置。然后加上enablewebmvcsecurity注解启动secure配置。至于enableglobalmethodsecurity注解加不加都可以,开启方法级别配置的。
到现在secure就添加好了而且可以正常工作,但是使用的是默认配置:
protected void configure(HttpSecurity http) throws Exception { logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity)."); http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin().and() .httpBasic(); }
默认配置说所有的请求都需要进行安全认证,登陆使用默认的form表单(会弹出一个可怕的登陆框),并使用http Basic 认证,就是通过密码和角色。
我们重新这个方法,并根据自己的需要增加配置:
http.authorizeRequests().antMatchers("/assets/", "/").permitAll() .anyRequest().authenticated() .and().formLogin().usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login").loginPage("/login") .and().logout().permitAll().logoutUrl("/logout").logoutSuccessUrl("/login") .logoutSuccessHandler(logoutSuccessHandler) .invalidateHttpSession(true).addLogoutHandler(logoutHandler).deleteCookies(new String[] { "cookie名字" }) .and().rememberMe();
第一行是说访问/和匹配/assets/**模式的url都可以直接访问,下面说其他的需要认证。使用form表单登录,为什么要指定这个呢?因为spring会从请求中查找username和password域参数。从哪个请求中呢?默认是/login,可以通过login(String s)自定义。表单提交的参数也可以通过usernameParameter()和passwordParamter()自定义。如果不使用默认的弹出框而使用自己的页面,表单的action必须和loginProcessingUrl()指定的一样,当然也需要是post方式。
再往下是允许spring控制登出。默认访问/logout会执行登出,spring会使session无效,并清理rememberMe生成的cookie。logoutUrl()可以自定义登出的url,成功登出后的跳转url由logoutSuccessUrl()指定,默认是/login?logout,你可以这个页面判断有Logout参数就提示用户登出成功。
再往后配置自动登录,使用rememberMe()方法。自动登录有两种方法,一个是由org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices类配置,另一个由org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices配置,差别是用不用数据库。
登录认证需要在这个类中新建一个autowired方法configureGlobal(auth):
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource) .usersByUsernameQuery("select username,password, enabled from users where username=?") .authoritiesByUsernameQuery("select username, role from user_roles where username=?"); }
有四种方法,第一种使用内存配置,就是直接使用常量串,我感觉用的很少;第二种是上面这段代码用的jdbc方式,第三种是ldap方式;第四种是使用userDetailsService:
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); auth.eraseCredentials(false).userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); }
因为需要自动登录,就把eraseCredentials设为false。
到现在为止,就把登录认证包括自动登录和url访问认证以及登出配好了。可以往数据库加入一个用户试一下,刚才看到登录的时候使用了passwordEncoder,因此插入用户的时候也要这样加密密码。
相关推荐
springboot集成cmybaits-freemark项目基本结构demo,集成Eureka微服务的注册发现与消费服务demo,集成Ribbon负载均衡及应用demo,集成hystrix等断路由(雪崩效应)demo......
在SpringBoot中集成flowable流程引擎及modeler可视化流程设计器项目完整源码,附有使用说明文档 1、使用开发工具eclipse或者idea等导入项目myflow 2、配置application.properties中的数据库连接,修改用户名和登录...
包含minio 安装步骤、springboot 如何集成minio,封装的minio 操作工具类,增加方法设置桶的权限
springboot集成datax的demo,下载及能运行
spring websocket在springboot中使用,超详细的使用方法,有实例代码
Spring cloud与Spring boot完整案例分析,注解详细,希望对大家有帮助。
SpringCloud+SpringBoot+OAuth2+Spring Security+Redis实现的微服务统一认证授权
springboot工程集成mq业务,配置及调用,便于理解,适合新手。
帮助开发者在Spring Boot中快速集成RocketMQ。支持Spring Message规范,方便开发者从其它MQ快速切换到RocketMQ。
spring-boot-中文参考手册 SpringBoot中文文档 springboot 中文 文档 SpringBoot 帮助您创建可以独立运行的、基于 Spring 的生产级应用程序。我们对 Spring 平台和第三方库有自己的看法,所以您可以从最简单的开始。...
springboot集成tdengine中连接区分,可直接运行
Springboot+clickhouse 集成demo,适用于各个版本
ignite集成及使用ignite实现分布式缓存、分布式锁、消息订阅发布、关系型数据库缓冲层等demo
基于 SpringCloud + SpringBoot + vue 构建的即时通讯系统源码.zip
spring框架集成socket服务,监听器检测到容器启动完成,通过线程启动socket服务
springCloud+springboot+spring+springMvc+mybatis+oracle基础框架项目,用于学习springcloud+springboot的整合
SpringBoot集成PageOffice
springboot2.0中使用java配置来实现springbatch,摒弃之前的xml文件配置。
springboot 集成oauth2.0服务器,基于oauth2.0授权码形式集成
lamp-cloud 基于Jdk11 + SpringCloud + SpringBoot 开发的微服务中后台快速开发平台,专注于多租户(SaaS架构)解决方案,亦可作为普通项目(非SaaS架构)的基础开发框架使用,目前已实现插拔式数据库隔离、SCHEMA隔离...