c# 自定义验证登录(Authorize)

我们的项目本来是用azure的auth认证,是用过程中发现登录速度太慢了,所以还是自己搞一个吧,没想到搞起来挺简单的,不是用一个专门的认证服务器哈,就是一个简单的工具类。

验证是否登录的类

    /// <summary>
    /// 认证类继承
    /// </summary>
    public class RequestAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            // 是否不需要验证 或者 已经登录
            if (SkipAuthorization(actionContext) || IsLogin(actionContext))
                return;

            actionContext.Response = GetResponse();
        }

        /// <summary>
        /// 返回信息接口
        /// </summary>
        private HttpResponseMessage GetResponse()
        {
            var response = ServiceResponse<bool>.WarningResponse(401, CommonConst.Msg_NoLogin, false);
            return JsonHelper.ToHttpResponseMessage(response);
        }

        /// <summary>
        /// 判断是否匿名使用接口
        /// </summary>
        private static bool SkipAuthorization(HttpActionContext actionContext)
        {
            if (!actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any<AllowAnonymousAttribute>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,可以使用 Spring Security 来实现用户认证和授权。下面是一个自定义用户登录的示例: 1. 添加依赖 在 pom.xml 文件中添加 Spring Security 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建用户类 创建一个用户类,用于存储用户信息。可以实现 Spring Security 的 UserDetails 接口,也可以不实现。 ```java public class User { private String username; private String password; // getters and setters } ``` 3. 创建用户服务 创建一个用户服务类,用于提供用户信息。可以从数据库或其他数据源中获取用户信息。 ```java @Service public class UserService { public User findByUsername(String username) { // 从数据库或其他数据源中获取用户信息 return new User(username, "password"); } } ``` 4. 实现 UserDetailsService 实现 Spring Security 的 UserDetailsService 接口,用于从用户服务中获取用户信息,并将其转换为 Spring Security 可以使用的 User 对象。 ```java @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserService userService; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userService.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.emptyList()); } } ``` 5. 配置 Spring Security 在配置类中,配置 Spring Security,包括登录页面、登录请求、登录成功和失败处理等。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .usernameParameter("username") .passwordParameter("password") .defaultSuccessUrl("/") .permitAll() .and() .logout() .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } } ``` 6. 创建登录页面 创建一个登录页面,用于输入用户名和密码。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <p> <label for="username">Username:</label> <input type="text" id="username" name="username" /> </p> <p> <label for="password">Password:</label> <input type="password" id="password" name="password" /> </p> <p> <input type="submit" value="Login" /> </p> </form> </body> </html> ``` 以上就是一个简单的 Spring Boot 自定义用户登录的示例。需要注意的是,这里的示例没有使用加密算法对密码进行加密,实际应用中应该对密码进行加密存储。同时,还应该在登录成功后将用户信息存储到 session 或 token 中,以便后续访问需要认证的资源时进行判断。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值