1.1SpringSecurity 是Spring家族一员,是一个能够为基于spring的企业应用系统提供声明式的安全访问控制解决方方案的安全框架,它提供了一组可以在Spring应用上下问中配置的Bean,充分利用了Spring Ioc,DI和Aop功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。(有了Springboot后使用的更广泛一些,可以高度自定义)
1.2Apache Shiro 一个功能强大且易于使用的java安全架构,提供了认证,授权加密和会话管理等,(轻量级的)
Demo链接:
2.1.SpringSecurity如何修改默认登录逻辑和,相关页面和密码等,一、自定义登录逻辑,新增一个User继承UserDetails和实现UserDetailsService接口,在UserDetailsService
的loadUserByUsername方法里面进行账号密码等进行校验,然后范围权限(多个用,分割,角色前面必须是ROLE_前缀)
二、密码可以用PasswordEncoder进行加密
三、自定义登录页面和登录失败页面:实现WebSecurityConfigurerAdapter,实现configure(HttpSecurity http)方法。
http的相关配置:
//自定义登录表单
http.formLogin()
// .loginPage("/login.html")
// //自定义登录逻辑
// .loginProcessingUrl("/login")
// //登录成功后跳转页面,post请求
// .successForwardUrl("/toMain")
// //.successHandler(new MyAuthenicationSuccessHandler(“main.html”))
// //登录失败跳转错误页
// .failureForwardUrl("/toError");
// // .defaultSuccessUrl("/").failureHandler(loginFailureHandle);
// //.failureHandler(new MyAuthenticationFailureHandler("/error.html"))
// ;
// //授权
// http.authorizeRequests()
// //放行/login.html
// .antMatchers("/login.html").permitAll()
// .antMatchers("/error.html").permitAll()
// .antMatchers("/druid/").permitAll()
// //角色控制权限
// //.antMatchers("/main1.html").hasRole(“cc”)
//
// // .antMatchers("/main1.html").access(“hasRole(‘bb’)”)
// //权限控制
// // .antMatchers("/main1.html").hasAuthority(“Admin1”)
// //放行/error.html
// // .antMatchers("/error.html").permitAll()
// //放行images目录图片
// //.antMatchers("/images/").permitAll()
// //.antMatchers("*.jpg").permitAll()
// // .regexMatchers(".+[.]jpg").permitAll()
// //认证拦截所有请求,必须登录
// .anyRequest().authenticated();
Springboot启动类加上prePostEnabled = true
prePostEnabled访问前验证
OAuth协议为用户资源的授权提供安全的
开放而又简易的标准
在这里插入图片描述
JJWT是java实现jwt标准的一个jar包
4.SpringSecurity整合JWT一.TokenStore调用获得加盐的JwtTokenStore
二.JwtAccessTokenConverter当成功获得Token后加盐,在转换为jwtToken
三.然后configure方法写endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailService)
.tokenStore(tokenStore)
.accessTokenConverter(jwtAccessTokenConverter);
一.创建一个服务端oauth2-client1-demo,要引入下面的jar包
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.cloud
spring-cloud-starter-security
二.然后application.properties配置
#应用服务 WEB 访问端口
server.port=8081
#防止cookie冲突,冲突会导致登录验证不通过
server.servlet.session.cookie.name=OAUTH2-CLINET-SESSIONID01
#授权服务器地址
oauth2-server-url: http://localhost:8080
#与授权服务器对应配置
security.oauth2.client.client-id=client
security.oauth2.client.client-secret=123456
security.oauth2.client.user-authorization-uri=
o
a
u
t
h
2
−
s
e
r
v
e
r
−
u
r
l
/
o
a
u
t
h
/
a
u
t
h
o
r
i
z
e
s
e
c
u
r
i
t
y
.
o
a
u
t
h
2.
c
l
i
e
n
t
.
a
c
c
e
s
s
−
t
o
k
e
n
−
u
r
i
=
{oauth2-server-url}/oauth/authorize security.oauth2.client.access-token-uri=
oauth2−server−url/oauth/authorizesecurity.oauth2.client.access−token−uri={oauth2-server-url}/oauth/token
security.oauth2.resource.jwt.key-uri=${oauth2-server-url}/oauth/token_key
三.写一个可以调用的资源Controller(接口/user/getCurrentUser)
四.客服端AuthorizationServerConfig重写 configure(AuthorizationServerSecurityConfigurer security)
//配置密钥 单点登录必须要配置的
security.tokenKeyAccess(“isAuthenticated()”);
五.configure(ClientDetailsServiceConfigurer clients)方法重定向到客服端的登录页
.redirectUris(“http://localhost:8081/login”)
//自动授权
.autoApprove(true)
六.启动服务端和客服端访问http://localhost:8081/user/getCurrentUser



