problem description
in the process of integrating springboot and shiro, I found that after successful login, I could not jump to the url configured by shiro. Instead, I directly returned the message of controller. Please give me your advice
.the environmental background of the problems and what methods you have tried
related codes
/ / Please paste the code text below (do not replace the code with pictures)
@ Configuration
public class ShiroConfiguration {
/**
* LifecycleBeanPostProcessorDestructionAwareBeanPostProcessor
* org.apache.shiro.util.Initializablebean
* AuthorizingRealmEhCacheManager
*/
@Bean(name = "lifecycleBeanPostProcessor")
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
/**
* DefaultAdvisorAutoProxyCreatorSpringbeanAdvisorAOP
*/
@Bean
@ConditionalOnMissingBean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
defaultAAP.setProxyTargetClass(true);
return defaultAAP;
}
/**
* FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
filterRegistration.setEnabled(true);
filterRegistration.addUrlPatterns("/*");
filterRegistration.setDispatcherTypes(DispatcherType.REQUEST);
return filterRegistration;
}
/**
* HashedCredentialsMatcher
*
* form
*/
@Bean(name = "hashedCredentialsMatcher")
public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
credentialsMatcher.setHashAlgorithmName("MD5");
credentialsMatcher.setHashIterations(2);
credentialsMatcher.setStoredCredentialsHexEncoded(true);
return credentialsMatcher;
}
/**
* ShiroRealmAuthorizingRealm
* JdbcRealm
*/
@Bean(name = "shiroRealm")
@DependsOn("lifecycleBeanPostProcessor")
public ShiroRealm shiroRealm() {
ShiroRealm realm = new ShiroRealm();
/ / realm.setCredentialsMatcher (hashedCredentialsMatcher ());
return realm;
}
/ / * *
/ / * EhCacheManager, cache management. After a user has successfully logged in, cache the user information and permission information.
/ / * then put it into the user"s session every time the user requests. If this bean is not set, each request will query the database once.
/ / * /
/ / @ Bean (name = "ehCacheManager")
/ / @ DependsOn ("lifecycleBeanPostProcessor")
/ / public EhCacheManager ehCacheManager () {
/ / return new EhCacheManager ();
/ /}
/**
* SecurityManagersession
* //
*/
@Bean(name = "securityManager")
public DefaultWebSecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(shiroRealm());
/ / securityManager.setCacheManager (ehCacheManager ());
return securityManager;
}
/**
* ShiroFilterFactoryBeanfactorybeanShiroFilter
* securityManagerfiltersfilterChainDefinitionManager
*/
@Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager());
Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
/ / LogoutFilter logoutFilter = new LogoutFilter ();
/ / logoutFilter.setRedirectUrl ("/ login");
/ / filters.put ("logout", null);
/ / shiroFilterFactoryBean.setFilters (filters);
Map<String, String> filterChainDefinitionManager = new LinkedHashMap<String, String>();
filterChainDefinitionManager.put("/login", "anon");
filterChainDefinitionManager.put("/logout", "logout");
filterChainDefinitionManager.put("/**", "authc");
/ / filterChainDefinitionManager.put ("/ events/**", "authc, filterChainDefinitionManager.put [Rolle _ ADMIN]");
/ / filterChainDefinitionManager.put ("/ user/edit/**", "authc,perms [user:edit]"); / / for testing purposes, you can also read
/ / filterChainDefinitionManager.put ("/ * *", "anon") from the database or other configuration;
shiroFilterFactoryBean.setSuccessUrl("/");
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager);
return shiroFilterFactoryBean;
}
/**
* AuthorizationAttributeSourceAdvisorshiroAdvisor
* AopAllianceAnnotationsAuthorizingMethodInterceptor
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
AuthorizationAttributeSourceAdvisor aASA = new AuthorizationAttributeSourceAdvisor();
aASA.setSecurityManager(securityManager());
return aASA;
}
}
here is my realm and contrller code
private Logger logger = LoggerFactory.getLogger (this.getClass ());
@Autowired
private UserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
logger.info("doGetAuthorizationInfo+"+principalCollection.toString());
UserPO user = userService.findByUserName((String) principalCollection.getPrimaryPrincipal());
//principalssession key=userId value=principals
SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()),SecurityUtils.getSubject().getPrincipals());
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//
/ / userService.updateUserLogin (user);
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
logger.info("doGetAuthenticationInfo +" + authenticationToken.toString());
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String userName=token.getUsername();
logger.info(userName+token.getPassword());
UserPO user = userService.findByUserName(token.getUsername());
if (user != null) {
/ / byte [] salt = Encodes.decodeHex (user.getSalt ());
/ / ShiroUser shiroUser=new ShiroUser (user.getId (), user.getLoginName (), user.getName ());
//session
System.out.println(user.getPassword());
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("user", user);
return new SimpleAuthenticationInfo(userName,token.getPassword(),getName());
} else {
return null;
}
/ / return null;
}
/**
* Go login
* @param request
* @return
*/
@RequestMapping(value="login", method= RequestMethod.POST)
public String login(HttpServletRequest request, RedirectAttributes redriect) {
String account = request.getParameter("name");
String password = request.getParameter("password");
UsernamePasswordToken upt = new UsernamePasswordToken(account, password);
Subject subject = SecurityUtils.getSubject();
try {
subject.login(upt);
} catch (AuthenticationException e) {
e.printStackTrace();
redriect.addFlashAttribute("errorText", "!");
return "redirect:/login";
}
return "{\"Msg\":\"\",\"state\":\"success\"}";
}