package com.springboot.springboot.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* 认证和授权处理类
*/
@Configuration
public class MySecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
//依赖注入通用的用户服务类
@Autowired
private MyUserSecurityService myUserSecurityService;
//依赖注入加密接口
@Autowired
private PasswordEncoder passwordEncoder;
//依赖注入用户认证接口
@Autowired
private AuthenticationProvider authenticationProvider;
//依赖注入认证成功处理类 验证用户成功后处理不同用户跳转到不同的页面
@Autowired
private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
/**
* BCryptPasswordEncoder是PasswordEncoder的接口实现
* 实现加密功能
* */
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
/**
* DaoAuthenticationProvider 是 AuthenticationProvider的实现
* */
@Bean
public AuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider provide = new DaoAuthenticationProvider();
//不隐藏用户未找到异常
provide.setHideUserNotFoundExceptions(false);
//设置自定义认证方式 用户登录认证
provide.setUserDetailsService(myUserSecurityService);
//设置密码加密程序认证
provide.setPasswordEncoder(passwordEncoder);
return provide;
}
/***
* 用户认证
* */
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("自定义用户认证已生效");
//设置认证方式
auth.authenticationProvider(authenticationProvider);
}
/***
* 请求授权 用户授权操作
* */
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("自定义用户授权已生效");
//不需要认证的请求
http.authorizeRequests().antMatchers("/toLogin","/login","/static/**").permitAll()
//这里默认追加“ROLE_” /user/** 是控制器的请求匹配路径
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/admin/**").hasAnyRole("ADMIN","DBA")
//其他所有请求登录后才能访问
.anyRequest().authenticated()
.and()
//将输入的用户名和密码和授权的进行比较
.formLogin().loginPage("/login").successHandler(myAuthenticationSuccessHandler)
.usernameParameter("username")
.passwordParameter("password")
//登录失败
.failureUrl("/login?error")
.and()
//注销行为可任意访问
.logout().permitAll().and()
//指定异常处理页面
.exceptionHandling().accessDeniedPage("/deniedAccess");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
springboot整合springSecurity (868个子文件)
MySecurityConfigurerAdapter.class 6KB
MyUserServiceImpl.class 4KB
MyAuthenticationSuccessHandler.class 3KB
MyUserSecurityService.class 3KB
MyUser.class 2KB
LoginController.class 2KB
Permission.class 2KB
Role.class 1KB
PassWordTest.class 1KB
RolePermission.class 1KB
UserRole.class 1KB
MyUserMapper.class 992B
MyUserService.class 949B
SpringbootApplication.class 876B
RolePermissionMapper.class 572B
SpringbootApplicationTests.class 567B
PermissionMapper.class 556B
UserRoleMapper.class 548B
RoleMapper.class 532B
mvnw.cmd 7KB
bootstrap.min.css 120KB
bootstrap.min.css 120KB
style.min.css 97KB
style.min.css 97KB
easyui.css 56KB
easyui.css 56KB
easyui.css 56KB
easyui.css 56KB
easyui.css 56KB
easyui.css 56KB
easyui.css 54KB
easyui.css 54KB
easyui.css 53KB
easyui.css 53KB
animate.min.css 46KB
animate.min.css 46KB
font-awesome.min.css 26KB
font-awesome.min.css 26KB
tabs.css 9KB
tabs.css 9KB
tabs.css 9KB
tabs.css 9KB
tabs.css 9KB
tabs.css 9KB
tabs.css 8KB
tabs.css 8KB
tabs.css 7KB
tabs.css 7KB
mobile.css 7KB
mobile.css 7KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
color.css 4KB
color.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
icon.css 3KB
icon.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
共 868 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
JavaWeb开发者
- 粉丝: 4
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0