标签:spring

以下是与标签 “spring” 相关联的文章

Spring Boot + RestController 配置

RestController 类注解

  • 类注解
  • 根路径
@RestController
@RequestMapping("/v1")
public class RegistratonController {

Get 方法

  • 对应查询操作
  • 获取路径参数
  • 获取 URL 参数
  • 无 Post 内容
@GetMapping(value = "/orders/{product}")
public List<OrderItem> getOrder(@PathVariable String product,
                                @RequestParam("column") String column,
                                @RequestParam("value") String value) {

Post 方法

  • 对应 Add 操作
  • 获取路径参数
  • 获取 Post data
@PostMapping(value = "/orders/{product}")
public OrderItem addOrder(@PathVariable String product, @RequestBody String json) {

Put 方法

  • 一般对应 update 操作
  • 获取路径参数
  • 获取 Post data
@PutMapping(value = "/orders/{product}")
public OrderItem updateOrder(@PathVariable String product, @RequestBody String json) {

Delete 方法

  • 一般对应 Delete 操作
  • 获取路径参数
@DeleteMapping(value = "/orders/{product}/{id}")
public ResponseEntity<OrderItem> deleteOrder(@PathVariable String product, @PathVariable int id) {

Spring Boot + JDBC 配置

MySQL 配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/registration
    username: root
    password: 123456

DAO 注解

  • DAO 注解
  • jdbcTemplate 自动注入
@Repository
public class RegistrationDAOImpl implements BaseRegistrationDAO {

  private JdbcTemplate jdbcTemplate;

  @Autowired
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }
  ...
}

RowMapper 实现

class OrderMapper implements RowMapper<OrderItem> {
  @Override
  public OrderItem mapRow(ResultSet rs, int rowNum) throws SQLException {
    OrderItem orderItem = new OrderItem();
    orderItem.id = rs.getInt("id");
    orderItem.name = rs.getString("UserName");
    return orderItem;
  }
}

查询实现

public List<OrderItem> getOrderItem(String product, String column, String value) {
  String sql = "SELECT id, UserName from my_" + product + " WHERE " + column + "=?";
  return jdbcTemplate.query(sql, new Object[]{value}, new OrderMapper());
}

得到新插入行的 ID

GeneratedKeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(con -> {
      PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
      statement.setString(1, orderItem.name);
      statement.setString(2, orderItem.email);
      return statement;
    }, holder);
    orderItem.id = holder.getKey().intValue();

Spring Boot + Jpa 配置

H2 配置

spring:
  datasource:
    url: jdbc:h2:r:/myh2
    driver-class-name: org.h2.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: create
      naming:
        strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
    show-sql: true
  h2:
    console:
      enabled: true
      path: /console

Bean 扫描注解

  • bean 扫描路径
  • repository bean 扫描路径
  • entity bean 扫描路径
  • getBean method
@SpringBootApplication
@ComponentScan("com.fj")
@EnableJpaRepositories("com.fj.dao")
@EntityScan("com.fj.entity")
public class H2jpa01Application {

  public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(H2jpa01Application.class, args);

    VideoService service = context.getBean(VideoService.class);
    service.addFile("test.mp4");
  }
}

Entity 注解

  • 表注解
  • 自增 ID 注解
  • 列注解
@Entity
public class Video {

  @Id
  @GeneratedValue
  private long id;

  @Column
  private Date gmtCreate;

  @Column
  private Date gmtModify;

  @Column
  private String fileName;

  ...//getter and setter
}

Repository 注解

  • Repository 注解
  • 声明为 interface
  • 模板参数为 Entiry, ID 数据类型
@Repository
public interface VideoDAO extends CrudRepository<Video, Long> {
}

Service 注解

  • Service 注解
  • DAO 自动注入
  • DAO 方法调用
@Service
public class VideoService {

  @Autowired
  private VideoDAO videoDAO;

  public void addFile(String fileName) {
    Video video = new Video(fileName);
    videoDAO.save(video);
  }
}

Spring Boot + Spring Security + Angular 配置

最简单配置

  • 弹出登录对话框
  • 自定义用户名密码
@Configurable
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) {
        try {
            httpSecurity.authorizeRequests().anyRequest().authenticated()
                    .and()
                    .httpBasic();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Autowired
    public void ConfigureGlobal(AuthenticationManagerBuilder auth) {
        try {
            auth.inMemoryAuthentication().withUser("root").password("123456").roles("ADMIN");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

自定义 LoginForm

  • 自定义 form 提交地址
  • 认证结果不跳转
  • 登录对话框中,允许资源访问
@Configurable
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthFailureHandler customAuthFailureHandler;

    @Autowired
    private CustomLoginSuccessHandler customLoginSuccessHandler;

    @Autowired
    private CustomLogoutSuccessHandler customLogoutSuccessHandler;

    Gson gson = new Gson();

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login").loginProcessingUrl("/auth")
                    .successHandler(customLoginSuccessHandler)
                    .failureHandler(customAuthFailureHandler)
                    .and()
                .logout().permitAll().logoutSuccessUrl("/logout")
                    .logoutSuccessHandler(customLogoutSuccessHandler)
        ;
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().regexMatchers(".*\\.js", ".*\\.css", ".*\\.ico", ".*\\.woff2");
    }

    @Autowired
    public void ConfigureGlobal(AuthenticationManagerBuilder auth) {
        try {
            auth.inMemoryAuthentication()
                    .withUser("root").password("123456").roles("ADMIN");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected class NoRedirectStrategy implements RedirectStrategy {
        @Override
        public void sendRedirect(HttpServletRequest request,
                                 HttpServletResponse response, String url) throws IOException {
            // no redirect
        }
    }

    @Component
    public class CustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

        public CustomLoginSuccessHandler() {
            super();
            setRedirectStrategy(new NoRedirectStrategy());
        }

        @Override
        public void onAuthenticationSuccess(HttpServletRequest request,
                                            HttpServletResponse response,
                                            Authentication authentication)
                throws ServletException, IOException {
            super.onAuthenticationSuccess(request, response, authentication);
            Writer writer = response.getWriter();
            writer.write(gson.toJson("success"));
        }
    }

    @Component
    public class CustomAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {

        public CustomAuthFailureHandler() {
            super();
            setRedirectStrategy(new NoRedirectStrategy());
        }

        @Override
        public void onAuthenticationFailure(HttpServletRequest request,
                                            HttpServletResponse response,
                                            AuthenticationException exception)
                throws IOException, ServletException {
            String error = exception.getMessage();
            //getRedirectStrategy().sendRedirect(request, response, "/login?error=" + error);
            Writer writer = response.getWriter();
            writer.write(gson.toJson(error));
        }
    }

    @Component
    public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
        @Override
        public void onLogoutSuccess(HttpServletRequest request,
                                    HttpServletResponse response,
                                    Authentication authentication)
                throws IOException, ServletException {
            Writer writer = response.getWriter();
            writer.write(gson.toJson("success"));
        }
    }
}

Angular 提交

  • 登录
onLogin() {
    const url = '/auth';
    // let data = {username: this.username, password: this.password };
    // let data = "username=" + this.username + "&password=" + this.password;
    let params = new URLSearchParams();
    params.set('username', this.username);
    params.set('password', this.password);

    let header = {
      headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
    };

    this.http.post(url, params.toString(), header).subscribe(
      data => {
        console.log(data);
        if (data != null && data == 'success') {
          this.router.navigateByUrl("/");
        } else {
          let snackBarRef = this.snackBar.open("Login failed. " + data, "CLOSE", {
            duration: 3000
          });
          snackBarRef.onAction().subscribe();
        }
      },
      err => {
        console.log("error: " + err.errorMsg);
      }
    );
  }
  • 登出
logout() {
  this.http.get('/logout').subscribe( data => {
    this.router.navigateByUrl('/login');
  });
}

angular2 学习笔记(1)- 工具配置和框架搭建

搭建 springboot 工程

工程框架

  • File -> New -> Project -> Spring Initializer
  • Dependencies: Web->Web, Template Engine->Thymeleaf
  • Thymeleaf 修改为 LEGACY 模式
    • pom.xml 中添加依赖

      <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
      </dependency>
      
    • 修改 application.properties

      spring.thymeleaf.mode=LEGACYHTML5
      spring.thymeleaf.cache=false
      

Bean 配置

  • 添加类 BeanConfig

    @Configuration
    @ImportResource("classpath:bean.xml")
        public class BeanConfig {
    }
    
  • 在 src->main->resources 下添加文件 bean.xml,并添加 Bean 实例

    <bean name="myBean" class="com.fj.springboot01.MyBean"/>
    

添加路由

  • 给类添加注解 @Controller
  • 给类添加方法

    @RequestMapping("/hello")
    public String hello() {
      System.out.println("hello mapping");
      return "hello";
    }
    
  • 添加 hello.html ,放在 src/main/resources/templates 目录下

添加静态资源

  • angular2 生成的页面作为静态资源放在 src/main/resources/static 目录下

搭建 angular2 框架

安装软件

  • 下载并安装 node.js LTS 版本到 D:/nodejs 目录
  • 安装 cnpm

    npm install -g cnpm --registry=https://registry.npm.taobao.org
    
    
  • 修改 C:/Users/fj/.npmrc

    registry=https://registry.npm.taobao.org/
    prefix=D:\nodejs
    cache=D:\nodejs\node_cache
    
  • 安装 npm 软件包

    npm install -g @angular/cli webpack typescript tslint
    
    

工程框架

  • 新建代码框架,下载依赖

    cd spring001/src/main
    ng new angular
    cd angular
    npm install
    
  • 新建 Component

    ng g c component/data-grid
    
    
  • 修改页面

编译

  • 命令行编译生成目标文件

    ng build --prod --aot --bh /note/ --no-extract-license
    
    
  • 修改 package.json , 添加两行

    "scripts": {
      "develop":"ng build -bh /note/ -w",
      "release":"ng build -bh /note/ -prod -aot --env=prod --no-extract-license"
    },
    
  • 将目标文件移动到静态资源目录下, 修改 .angular-cli.json

    "outDir": "../resources/static/note", 
    

调试

  • tsconfig.json 中打开 sourceMap 选项

    "sourceMap": true,
    "inlineSources": true,
    
  • 在 IDEA 中直接创建 SpringBoot 启动项
  • 启动调试