您的位置:首页 > 其它

CAS5.2x单点登录(三)------自定义登录验证

2018-02-21 17:23 295 查看
前面我们讲解了cas的简单连接数据库以及简单的加密,可是有时候我们的登录可能不是cas原有的逻辑,我们就必须要重新cas的登录验证以到达自己想要的效果,好在cas提供这些东西给我们自己重写。但是在重写这些之前,需要了解一下几个注解以及一个配置文件

注解:@Configuration、@EnableConfigurationProperties(CasConfigurationProperties.class)(这个是重写一些东西必须得加上的)。

配置文件:

,就是在resources新建一个mata-inf的文件夹,然后新建一个交spring.factories的文件。这里面存放的就是我们要放置的类(具体可以查看一下官方文档)。

下面正式开始我们的自定义登录验证的第一步。我们需要继承某些类,其实cas

已经帮我弄好了一些抽象类,比如AbstractUsernamePasswordAuthenticationHandler,或者是jdbc的认证,其实这些我们都可以自己继承然后重写。我们这边就用AbstractUsernamePasswordAuthenticationHandler这个作为例子把:

public class Login extends AbstractUsernamePasswordAuthenticationHandler {
private static final org.slf4j.Logger LOGGER =LoggerFactory.getLogger(Login.class);

public Login(String name, ServicesManager servicesManager, PrincipalFactory principalFactory,
Integer order) {
super(name, servicesManager, principalFactory, order);
// TODO Auto-generated constructor stub
}

//  private final String sql="select password from sec_user where username=?";
private String sql="select * from sec_user where username=?";

@Override
protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential transformedCredential,
String originalPassword) throws GeneralSecurityException, PreventedException {
// TODO Auto-generated method stub
DriverManagerDataSource d=new DriverManagerDataSource();
d.setDriverClassName("com.mysql.jdbc.Driver");
d.setUrl("jdbc:mysql://127.0.0.1:3306/test");
d.setUsername("root");
d.setPassword("root");
JdbcTemplate template=new JdbcTemplate();
template.setDataSource(d);

String username=transformedCredential.getUsername();
String pd=transformedCredential.getPassword();
//      //查询数据库加密的的密码
UserInfo user=template.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<UserInfo>(UserInfo.class));

//      if(sqlpd.equals(pd)){
//          return createHandlerResult(transformedCredential, principalFactory.createPrincipal(username, null), null);
//      }
if(user==null){
throw new FailedLoginException("没有该用户");
}
//返回多属性
Map<String, Object> map=new HashMap<>();
map.put("email", user.getEmail().toString());
map.put("status", user.getStatus().toString());
LOGGER.info(map.get("email").toString());
LOGGER.info("++++++++++++++++++++zjzjzjz",map);

if(PasswordUtil.decodePassword(user.getPassword(), pd, username)){
return createHandlerResult(transformedCredential, principalFactory.createPrincipal(username, map), null);
}
throw new FailedLoginException("Sorry, login attemp failed.");
//      return  null;
}

}


上面就完成了一个自定义登陆验证的以及返回多属性的问题。但是完成这个不等于我们自定义成功,接下来还需要将这个给注册进去,其代码如下:

@Configuration("MyAuthenticationConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class MyAuthenticationConfiguration implements AuthenticationEventExecutionPlanConfigurer {

@Autowired
private CasConfigurationProperties casProperties;

@Autowired
@Qualifier("servicesManager")
private ServicesManager servicesManager;

@Bean
public AuthenticationHandler myAuthenticationHandler() {
final Login handler = new Login(Login.class.getSimpleName(), servicesManager, new DefaultPrincipalFactory(), 10);
return handler;
}

@Override
public void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) {
// TODO Auto-generated method stub
plan.registerAuthenticationHandler(myAuthenticationHandler());
}

}


最后在配置文件里面加上这个注册类的具体路径以及类名:org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.zj.cas.MyAuthenticationConfiguration。

这时候就完成自定义登录验证。然后重新编译并且打包,然后放到tomcat里面运行,这个时候登录的逻辑就是你自己的自定义逻辑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cas单点登录