thisdcw commented on issue #2103:
URL: https://github.com/apache/shiro/issues/2103#issuecomment-2889596918
Or do you need the java file instead of a screenshot?
`package com.cw.shiro;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
/**
* @author thisdcw
*/
@SpringBootApplication
public class CwShiroApplication {
@Resource
private ApplicationContext applicationContext;
public static void main(String[] args) {
SpringApplication.run(CwShiroApplication.class, args);
}
@PostConstruct
public void setSecurityManager() {
SecurityManager securityManager = ThreadContext.getSecurityManager();
if (securityManager == null) {
SecurityUtils.setSecurityManager(applicationContext.getBean(SecurityManager.class));
}
}
}
`
`package com.cw.shiro.config;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author thisdcw
*/
@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
return new CustomRealm();
}
@Bean
public SecurityManager securityManager(Realm realm) {
DefaultSecurityManager securityManager = new
DefaultSecurityManager();
securityManager.setRealm(realm);
return securityManager;
}
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chain = new
DefaultShiroFilterChainDefinition();
chain.addPathDefinition("/login", "anon");
chain.addPathDefinition("/**", "authc");
return chain;
}
}
`
`package com.cw.shiro.config;
import com.cw.shiro.service.UserService;
import jakarta.annotation.Resource;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
/**
* @author thisdcw
*/
public class CustomRealm extends AuthorizingRealm {
@Resource
private UserService userService;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
token) throws AuthenticationException {
//mock get user from database
//has exception but doesn't have throw
userService.mockException();
return new SimpleAuthenticationInfo("admin", "123456", getName());
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection
principals) {
return new SimpleAuthorizationInfo();
}
}
`
`package com.cw.shiro.service;
import com.cw.shiro.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
/**
* @author thisdcw
*/
@Service
public class UserService {
public void mockException() {
User user = new User();
User user1 = new User();
//has exception: Source must not be null
BeanUtils.copyProperties(user, user1);
}
}
`
`package com.cw.shiro;
import lombok.Data;
/**
* @author thisdcw
*/
@Data
public class User {
private String name;
private String password;
}
`
`package com.cw.shiro.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author thisdcw
*/
@RestController
public class LoginController {
@PostMapping("/login")
public String login(@RequestParam String username, @RequestParam String
password) {
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
UsernamePasswordToken token = new
UsernamePasswordToken(username, password);
subject.login(token);
}
// Because there is an exception in the authenticator, success
should not be returned here, but it is returned
return "login success";
}
}
`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]