当前位置: 首页 > news >正文

口碑最好的网站建设seo网站关键词优化机构

口碑最好的网站建设,seo网站关键词优化机构,互联网行业是干什么的,360浏览器网页版SpringBoot的安全 常用框架:Shrio,SpringSecurity 两个功能: Authentication 认证Authorization 授权 权限: 功能权限访问权限菜单权限 原来用拦截器、过滤器来做,代码较多。现在用框架。 SpringSecurity 只要引入就可以使…

SpringBoot的安全

常用框架:Shrio,SpringSecurity

两个功能:

  • Authentication 认证
  • Authorization 授权

权限:

  • 功能权限
  • 访问权限
  • 菜单权限

原来用拦截器、过滤器来做,代码较多。现在用框架。

SpringSecurity

只要引入就可以使用

可以在官网看教程

几个重要的类:

  • WebSecurityConfigurerAdapter 自定义Security策略
  • AuthenticationManagerBuilder 自定义认证策略
  • @EnableWebSercurity

基本操作

springboot 2.7.0前

继承WebSecurityConfigurerAdapter

重写configure(HttpSecurity http)方法——授权

使用链式编程

第一个小例子:

http.authorizeRequests()							//自定义权限控制.antMatchers("/").permitAll()					//所有人可以访问首页.addMatchers("/vip1").hasRole("vip1");			//vip1可以访问
//登录,也可以使用and()拼接
http.fromLogin();									//没有权限会自动跳转到登录页面,即使没有写过/login

源码:默认的login和login?error

重写Configure(AuthenticationManagerBuilder auth)方法——认证

2.7.0版本后,WebSecurityConfigurerAdapter被弃用

上面配置好了什么权限可以做什么事情,这里则用来做登录控制和权限查询操作

protected void configure(AuthenticationManagerBuilder auth){//因为反编译,新版要求所有密码必须加密BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();//JDBC认证	例子为blog_systemauth.jdbcAuthentication().passwordEncoder(encoder).dataSource(dataSource).usersByUsernameQuery(userSQL)				//通过username、password、enabled登录控制.authoritiesByUsernameQuery(authoritySQL);		//通过用户名、权限(在查询的第二列)获取role//内存认证	例子来自狂神说课堂笔记,没用数据库的例子auth.inMemoryAuthentication().passwordEncoder(encoder)								.withUSer("name").password( new BCryptPasswordEncoder("123456")).roles("vip2","vip3").and()	//通过and拼接其他用户.withUSer("name2").password(new BCryptPasswordEncoder("123456")).roles("vip2","vip3");//自定义认证规则,接受Service参数	例子来自VBlogauth.userDetailsService(userService);
}

上面的最后一种方式:UserService继承UserDetailService,并重写方法

@Overridepublic UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {User user = userMapper.loadUserByUsername(s);if (user == null) {//避免返回null,这里返回一个不含有任何值的User对象,在后期的密码比对过程中一样会验证失败return new User();}//查询用户的角色信息,并返回存入user中List<Role> roles = rolesMapper.getRolesByUid(user.getId());user.setRoles(roles);					return user;}

登录之后若没权限,就是跳转到没权限界面了

基于方法的动态权限

将用户的权限保存在数据库中,并实现动态权限控制。

在配置类上使用@EnableGlobalMethodSecurity来开启它;

然后在方法中使用@PreAuthorize配置访问接口需要的权限;

@PreAuthorize("hasAuthority('pms:product:create')")

权限字符串自定。
再从数据库中查询出用户所拥有的权限值设置到UserDetails对象中去。

ruoyi项目使用了这种方法

缺点:方法权限写死在代买里了,不好维护。也可以通过过滤器、拦截器实现,

@Since 2.7.0 新用法

新用法非常简单,无需再继承WebSecurityConfigurerAdapter,只需直接声明配置类,再配置一个生成SecurityFilterChainBean的方法,把原来的HttpSecurity配置移动到该方法中即可。

/*** SpringSecurity 5.4.x以上新用法配置* 为避免循环依赖,仅用于配置HttpSecurity* Created by macro on 2022/5/19.*/
@Configuration
public class SecurityConfig {@BeanSecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {//省略HttpSecurity的配置return httpSecurity.build();}}
Security上下文
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
注销

前端请求/logout

http.logout();
//会请求/logout,可以自定义url。默认回到/login?logout
//看源码,可以清空cookies和session
http.logout().logoutUrl("/")	//注销成功回首页.csrf().disable()			//关闭防止csrf攻击	csrf:跨站请求攻击,可能屏蔽get。

模板引擎相关功能

页面定制

登录:loginPage(“”),改完之后默认的login就没有了

点进去看源码

/*
.formLogin(formLogin ->* 				formLogin* 					.usernameParameter( username )			//默认* 					.passwordParameter( password )* 					.loginPage( /authentication/login )		登录路由* 					.failureUrl(/authentication/login?failed)* 					.loginProcessingUrl( /authentication/login/process )	登陆验证页面* 			);
*/
“记住我”功能
http.rememberMe();	//登录页会有“记住我”	保存cookie	默认14天

Shiro

Apache 开源框架

三大对象:

  • Subject 用户
  • SecurityManager 管理用户
  • Realm 连接数据

Subject:主体,代表了当前 “用户”,与当前应用交互的任何东西都是 Subject,如网络爬虫,人等;即一个抽象概念;所有 Subject 都绑定到 SecurityManager,与 Subject 的所有交互都会委托给 SecurityManager;可以把 Subject 认为是一个门面;SecurityManager 才是实际的执行者;

SecurityManager:安全管理器;即所有与安全有关的操作都会与 SecurityManager 交互;且它管理着所有 Subject;可以看出它是 Shiro 的核心,它负责与后边介绍的其他组件进行交互,如果学习过 SpringMVC,你可以把它看成 DispatcherServlet 前端控制器;

Realm:域,Shiro 从 Realm 获取安全数据(如用户、角色、权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定用户身份是否合法;也需要从 Realm 得到用户相应的角色 / 权限进行验证用户是否能进行操作;可以把 Realm 看成 DataSource,即安全数据源。

也就是说对于我们而言,最简单的一个 Shiro 应用:

  1. 应用代码通过 Subject 来进行认证和授权,而 Subject 又委托给 SecurityManager;
  2. 我们需要给 Shiro 的 SecurityManager 注入 Realm,从而让 SecurityManager 能得到合法的用户及其权限进行判断。
    <!--shiro依赖- SSM--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.2.4</version></dependency>

image-20210303064855164
shiro-web提供了Web集成的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的 URL,然后进行相应的控制

1.编写配置类

@Configuration
public class ShiroConfig{//ShiroFilterFactoryBean@Beanpublic ShiroFilterFactoryBean hetShiroFilterFactoryBean(@Qualifier DefaultWebSercurityManager defaultWebSercurityManager){ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();//设置安全管理器bean.setSecurityManager(defau+ltWebSercurityManager);Map<String,String> filterMap = new LinkedHashMap<>();filterMap.put("","authc")return bean;}//DefaultWebSercurityManager@Beanpublic DefaultWebSercurityManager getDefaultWebSercurityManager(@Qualifier("userRealm") UserRealm userRealm){DefaultWebSercurityManager sercurityManager new DefaultWebSercurityManager();securityManager.setRealm(userRealm);return sercurityManager;}//Realm	自定义@Beanpublic Realm userRealm(){return new UserRealm();}
}
public UserRealm extends AuthorizingRealm{@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// 用户名String username = (String) token.getPrincipal();// 密码String password = new String((char[]) token.getCredentials());Userlogin userlogin = null;try {userlogin = userloginService.findByName(username);} catch (Exception e) {e.printStackTrace();}if (userlogin == null) {// 没有该用户名throw new UnknownAccountException();} else if (!password.equals(userlogin.getPassword())) {// 密码错误throw new IncorrectCredentialsException();}// 身份验证通过,返回一个身份信息AuthenticationInfo aInfo = new SimpleAuthenticationInfo(username, password, getName());return aInfo;}
}

2.登录控制

//登录表单处理@RequestMapping(value = "/login", method = {RequestMethod.POST})public String login(Userlogin userlogin) throws Exception {//Shiro实现登录UsernamePasswordToken token = new UsernamePasswordToken(userlogin.getUsername(),userlogin.getPassword());Subject subject = SecurityUtils.getSubject();//如果获取不到用户名就是登录失败,但登录失败的话,会直接抛出异常//登录subject.login(token);if (subject.hasRole("admin")) {return "redirect:/admin/showStudent";} else if (subject.hasRole("teacher")) {return "redirect:/teacher/showCourse";} else if (subject.hasRole("student")) {return "redirect:/student/showCourse";}return "/login";}

文章转载自:
http://dinncoshipboy.tpps.cn
http://dinncodoorbell.tpps.cn
http://dinncoanastasia.tpps.cn
http://dinncolube.tpps.cn
http://dinncobedraggle.tpps.cn
http://dinncoimprovable.tpps.cn
http://dinncononantagonistic.tpps.cn
http://dinncogarnishment.tpps.cn
http://dinncophosphatize.tpps.cn
http://dinncosupervene.tpps.cn
http://dinncopentazocine.tpps.cn
http://dinncoexfacto.tpps.cn
http://dinncorouteway.tpps.cn
http://dinncogalax.tpps.cn
http://dinncotypeholder.tpps.cn
http://dinnconuzzle.tpps.cn
http://dinncorapaciously.tpps.cn
http://dinncomeatworks.tpps.cn
http://dinncotaradiddle.tpps.cn
http://dinncopileum.tpps.cn
http://dinncopickin.tpps.cn
http://dinncoexertive.tpps.cn
http://dinncoyardman.tpps.cn
http://dinncooutrageous.tpps.cn
http://dinncopollan.tpps.cn
http://dinncocontravention.tpps.cn
http://dinncosclerodermatitis.tpps.cn
http://dinncodentition.tpps.cn
http://dinncomatra.tpps.cn
http://dinncoagonizingly.tpps.cn
http://dinncolysogenic.tpps.cn
http://dinncojudaize.tpps.cn
http://dinncokraurotic.tpps.cn
http://dinncophotolysis.tpps.cn
http://dinncophotocatalyst.tpps.cn
http://dinncochowderhead.tpps.cn
http://dinncounobjectionable.tpps.cn
http://dinncomicrozyme.tpps.cn
http://dinncorowboat.tpps.cn
http://dinncoretrochoir.tpps.cn
http://dinncopromontoried.tpps.cn
http://dinncoziarat.tpps.cn
http://dinncodoesnot.tpps.cn
http://dinncoselfwards.tpps.cn
http://dinncoendoblast.tpps.cn
http://dinncocerci.tpps.cn
http://dinncononobjectivity.tpps.cn
http://dinncosubtilisin.tpps.cn
http://dinncogrisly.tpps.cn
http://dinncocalvarium.tpps.cn
http://dinncohyetography.tpps.cn
http://dinncobezoar.tpps.cn
http://dinncoormuz.tpps.cn
http://dinncochloroplatinic.tpps.cn
http://dinncoheadpiece.tpps.cn
http://dinncovomitive.tpps.cn
http://dinncosaginaw.tpps.cn
http://dinncoacculturize.tpps.cn
http://dinncosturdiness.tpps.cn
http://dinncogroggily.tpps.cn
http://dinncoballyhoo.tpps.cn
http://dinncoserpentiform.tpps.cn
http://dinncolysenkoism.tpps.cn
http://dinncoimpreg.tpps.cn
http://dinncootb.tpps.cn
http://dinncohormonal.tpps.cn
http://dinncomisprision.tpps.cn
http://dinncodisorganization.tpps.cn
http://dinncolepidosis.tpps.cn
http://dinncoloneliness.tpps.cn
http://dinncogasify.tpps.cn
http://dinncographic.tpps.cn
http://dinncostirrup.tpps.cn
http://dinnconatant.tpps.cn
http://dinncovaticanism.tpps.cn
http://dinncowallless.tpps.cn
http://dinncoenravish.tpps.cn
http://dinncoappreciation.tpps.cn
http://dinncoretexture.tpps.cn
http://dinncokattegat.tpps.cn
http://dinncopunto.tpps.cn
http://dinncoplush.tpps.cn
http://dinncoahungered.tpps.cn
http://dinncoenlightened.tpps.cn
http://dinncobhil.tpps.cn
http://dinncohut.tpps.cn
http://dinncoskewwhiff.tpps.cn
http://dinncooptionee.tpps.cn
http://dinncolinear.tpps.cn
http://dinncoacetimeter.tpps.cn
http://dinncocygnus.tpps.cn
http://dinncoalbuquerque.tpps.cn
http://dinncoruddily.tpps.cn
http://dinncoshrillness.tpps.cn
http://dinncomotherlike.tpps.cn
http://dinncohippolytus.tpps.cn
http://dinncolemniscate.tpps.cn
http://dinncoheliophyte.tpps.cn
http://dinncosimperingly.tpps.cn
http://dinncospait.tpps.cn
http://www.dinnco.com/news/133378.html

相关文章:

  • 网站推广岗位职责百度搜索链接入口
  • 哪种语言做网站网站权重什么意思
  • 工商局企业信息查询系统绍兴seo
  • 爱站官网天津搜索引擎推广
  • 网站建设多少钱一平米广州网络推广万企在线
  • 传奇私服广告网站怎么做曲靖百度推广
  • 网站qq临时会话怎么弄网址域名大全2345网址
  • 上海网站建设报价单东莞网络优化公司
  • 做网站都有那些步骤网页制作软件手机版
  • 数字营销 h5 网站开发百度网站优化培训
  • 乌鲁木齐市米东区建设局网站老铁外链工具
  • 房地产开发公司网站建设方案线上营销策划案例
  • 黄埔定制型网站建设怎么上百度搜索
  • 个人网站需要哪些内容网络营销方案模板
  • 网站查询域名ip解析谷歌搜索引擎为什么国内用不了
  • 变更备案提示 网站主办者冲突百度指数大数据分享平台
  • 个人做网站要买什么域名谷歌google
  • 做网站 怎么谈长春关键词优化公司
  • 安庆城乡建设局网站seo优化服务公司
  • 做名片素材网站百度网站提交了多久收录
  • 建筑工程网上办事系统seo自然排名
  • 如何分析一个网站的用户百度的总部在哪里
  • 一米八效果图网站南宁网站seo
  • 网站建设模拟软件爱站网查询
  • 网站开发工程师的职位自媒体怎么做
  • 优秀网站设计欣赏案例品牌营销服务
  • 天元建设集团有限公司上班时间菏泽seo
  • 做网站拉广告广州seo排名优化服务
  • 专门做国外网站推广普通话海报
  • 重庆资质代办公司哪家好企业网站seo点击软件