java 注销用户登录,java-仅向登录的用户显示注销按钮

I've searched the web for a solution, but I can't figure this out.

I'm using Spring Boot with Maven and have the

spring-boot-starter-thymeleaf dependency in my pom.xml file.

I'm trying to show the logout button, using Thymeleaf, only if the user is logged in, but this code doesn't seem to work:

It keeps showing this error:

There was an unexpected error (type=Internal Server Error, status=500).

Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')"

and the line number where I have the "if" condition with thymeleaf.

How can I resolve this ?

解决方案

You could use the sec:authorize="isAuthenticated()", but you might have to add the dependencies etc for this. I used:

org.thymeleaf.extras

thymeleaf-extras-springsecurity4

2.1.2.RELEASE

compile

Also, make sure to add the SpringSecurityDialect so that isAuthenticated and other similar expressions can be evaluated by Spring. ex:

Bean

public SpringTemplateEngine templateEngine() {

SpringTemplateEngine engine = new SpringTemplateEngine();

engine.setTemplateResolver(templateResolver());

engine.addDialect(new SpringSecurityDialect());

return engine;

}

To the html page itself, add the following to the tag to make sure the sec: tag is recognized:

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro是一个强大的Java安全框架,可以用于认证、授权、加密、会话管理等方面。下面是使用Shiro完成用户登录认证功能的步骤: 1. 引入Shiro依赖 在项目的pom.xml中引入Shiro的依赖: ``` <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.7.1</version> </dependency> ``` 2. 配置Shiro 在项目的配置文件中配置Shiro,包括Realm、Session管理器、密码比较器等: ``` @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl("/login"); shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized"); Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/css/**", "anon"); filterChainDefinitionMap.put("/js/**", "anon"); filterChainDefinitionMap.put("/images/**", "anon"); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } @Bean public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(realm()); securityManager.setSessionManager(sessionManager()); return securityManager; } @Bean public Realm realm() { CustomRealm realm = new CustomRealm(); realm.setCredentialsMatcher(credentialsMatcher()); return realm; } @Bean public SessionManager sessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setGlobalSessionTimeout(30 * 60 * 1000); sessionManager.setDeleteInvalidSessions(true); sessionManager.setSessionValidationSchedulerEnabled(true); sessionManager.setSessionIdCookieEnabled(true); sessionManager.setSessionIdCookie(sessionIdCookie()); return sessionManager; } @Bean public SimpleCookie sessionIdCookie() { SimpleCookie cookie = new SimpleCookie("JSESSIONID"); cookie.setHttpOnly(true); cookie.setMaxAge(-1); return cookie; } @Bean public CredentialsMatcher credentialsMatcher() { HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); credentialsMatcher.setHashAlgorithmName("SHA-256"); credentialsMatcher.setHashIterations(1); return credentialsMatcher; } ``` 3. 自定义Realm 实现自己的Realm,用于认证用户的身份和权限: ``` public class CustomRealm extends AuthorizingRealm { @Autowired private UserService userService; @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); User user = userService.findByUsername(username); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(user.getRoles()); authorizationInfo.setStringPermissions(user.getPermissions()); return authorizationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); User user = userService.findByUsername(username); if (user == null) { throw new UnknownAccountException(); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, user.getPassword(), getName()); return authenticationInfo; } } ``` 4. 编写登录页面和处理登录请求的Controller 在登录页面中,用户可以输入用户名和密码,登录请求提交到后端进行处理: ``` @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String doLogin(String username, String password) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); return "redirect:/home"; } catch (AuthenticationException e) { return "login"; } } ``` 5. 编写注销功能 在需要注销的页面中,用户可以点击注销按钮,请求提交到后端进行处理: ``` @GetMapping("/logout") public String logout() { Subject subject = SecurityUtils.getSubject(); subject.logout(); return "redirect:/login"; } ``` 完成了以上步骤,就可以使用Shiro完成用户登录认证功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值