ByteSource newSalt = ByteSource.Util.bytes(pwd_salt); SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(uname, upwd,newSalt , "customRealm");
[main] #配置凭证匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher #设置凭证匹配器的相关属性 #设置匹配器的加密算法名称 credentialsMatcher.hashAlgorithmName=MD5 #设置匹配器迭代次数 credentialsMatcher.hashIterations=2 #配置realm customRealm=com.bjsxt.CustomRealm #配置Realm凭证匹配器属性 customRealm.credentialsMatcher=$credentialsMatcher #将Realm注入给securityManager securityManager.realm=$customRealm
这个对比,是把数据库的2ea27df2bf510143f5f07a441a62cfcf解密成123456来和程序里的123456对比?还是把程序里的123456加密成2ea27df2bf510143f5f07a441a62cfcf和数据库的对比?
我照着敲的,访问时候弹出这个
package com.security.handle; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.web.access.AccessDeniedHandler; import org.springframework.stereotype.Component; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * @author liufupeng * @date 2021/5/11 */ @Component public class MyAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException { httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); httpServletResponse.setContentType("application/json:charset=utf-8"); PrintWriter writer = httpServletResponse.getWriter(); writer.println("{\"code\":\"403\",\"msg\":\"无权限\"}"); writer.flush(); writer.close(); } }
package com.security.config; import com.security.handle.MyAccessDeniedHandler; import com.security.handle.MyAuthenticationFailHandler; import com.security.handle.MyAuthenticationSuccessHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; /** * @author liufupeng * @date 2021/5/8 */ @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyAccessDeniedHandler myAccessDeniedHandler; @Override protected void configure(HttpSecurity http) throws Exception { // 表单认证 http.formLogin() .loginProcessingUrl("/login") // 当发现/login时认为是登陆,需要执行UserDetailsServiceImpl // .successForwardUrl("/toMain") // 登陆成功 此处为post请求 // .failureForwardUrl("/fail") .usernameParameter("username") //自定义username字段 .passwordParameter("password") .successHandler(new MyAuthenticationSuccessHandler("/toMain")) .failureHandler(new MyAuthenticationFailHandler("/fail.html")) .loginPage("/login.html"); // url 拦截 http.authorizeRequests() .antMatchers("/login.html", "/fail.html").permitAll() // 登陆不需要被认证 // .antMatchers("/main1.html").hasAuthority("admin") .antMatchers("/main1.html").hasIpAddress("127.0.0.1") .anyRequest().authenticated(); http.csrf().disable(); http.exceptionHandling() .accessDeniedHandler(myAccessDeniedHandler); } @Bean public PasswordEncoder getPe() { return new BCryptPasswordEncoder(); } }
这样子授权等于其实就是取一个pojo对象中的菜单而已呀
跟权限没什么关系啊?
在rbac的学习中,权限的核心问题不是越级访问的问题么,
shiro没有什么权限过滤器吗
这一章,输入以下路径后,“localhost/reportform/find报403
老师,我请求localhost:80/main时浏览器跳转到localhost:80/login.html,但是浏览器返回404 ,IDEA中没有报错,这个要怎么解决
老师,那shiro里的这个session和我们一起用的那个HttpSession是不是同一个对象,shiro里的session销毁了是不是这次会话的session也没了?那如果是销毁了我们以前创建的那种session那是不是shiro里的session也没了?
跟上一个问题的同学一样是这个问题
老师,如果没有关闭csrf防护的话,想要页面正常访问,是form表单和ajax请求传入token就可以了吧,其他页面跳转还需要传token吗
如果userLogin表中存在多个数据,会创建多个simpleAuthenticationInfo对象吗?
老师,mysql-connector-j和mysql-connector-java是一样的效果吗
帮忙看下是我数据库写错了吗。不管怎么改都是登录失败。、
shiro-jdbcini如下:
[main] dataSou=com.mchange.v2.c3p0.ComboPooledDataSource dataSou.driverClass=com.mysql.jdbc.Driver dataSou.jdbcUrl=jdbc://127.0.0.1:3306/shiro dataSou.user=root dataSou.password=root jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm.dataSource=$dataSou securityManager.realm=$jdbcRealm
数据库代码:
CREATE TABLE `users` ( `username` varchar(50) NOT NULL, `password` int(50) NOT NULL, PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
测试代码:
package com.liyang.shiro1; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; public class Test2 { public static void main(String[] args) { // 1 解析shiro。ini Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-jdbc.ini"); // 2获得工厂实例 SecurityManager securityManager = factory.getInstance(); //3 把SecurityManager对象设置到运行环境中 SecurityUtils.setSecurityManager(securityManager); //4通过 SecurityUtils获得主体对象subject Subject subject = SecurityUtils.getSubject(); //5书写自己输入的账号和密码 UsernamePasswordToken token = new UsernamePasswordToken("sxt", "123"); //6验证密码 try { subject.login(token); //7 通过方法判断是否登录成功 if (subject.isAuthenticated()) { System.out.println("登录成功"); } } catch (AuthenticationException e) { System.out.println("登录失败"); //e.printStackTrace(); } }
输出结果:
按照老师的步骤导入项目,为什么输入用户名跟密码显示404
tomcat没有错误报警
数据库已经链接,测试通过
按F12没有错误报警
直接在网址上输入../success.jsp可以显示
老师我的只要一登录走的就是未知异常,路径也检查了
老师,我按您的步骤做完,出现这样的错误,您看一下
老师我和上面那个兄弟遇到了一样的问题,登录之后是错误页面但是手动补全完整的url就可以调到自定义页面了,请老师详解
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637